Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array push
(version: 0)
Comparing performance of:
push vs spread
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
push
var arr = [ 1, 2, 4 ]; arr.push(6);
spread
var arr = [ 1, 2, 4 ]; arr = [...arr, 6];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
81019216.0 Ops/sec
spread
13392446.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros/cons of different approaches. **Benchmark Overview** The benchmark measures the performance difference between two methods for pushing an element onto an array in JavaScript: 1. `push()` method 2. Spreading syntax (`arr = [...arr, 6];`) **Options Compared** The two options being compared are: * `push()`: The traditional method of appending a new element to an array using the `push()` function. * Spread syntax: A newer syntax introduced in ECMAScript 2015 (ES6) that allows creating a new array by spreading an existing array and adding a new element. **Pros and Cons** **Push() Method** Pros: * Wide support across different browsers and versions * Simple and straightforward implementation * Well-established and widely used Cons: * Can be slower for large arrays due to the overhead of function calls and array resizing **Spread Syntax (ES6)** Pros: * Faster performance for large arrays, as it avoids the overhead of function calls and array resizing * Simplifies code and reduces verbosity * Widely supported in modern browsers and Node.js versions Cons: * Requires ECMAScript 2015 or later support * May not work in older browsers or versions that don't support ES6 syntax **Library Use** There is no library explicitly mentioned in the benchmark definition. However, if we look at the test cases, we can see that both `push()` and spread syntax use a single array variable (`arr`) and modify its length. **Special JS Features/Syntax** The spread syntax uses the new syntax introduced in ECMAScript 2015 (ES6). This syntax allows creating a new array by spreading an existing array and adding a new element. The exact syntax used is `arr = [...arr, 6];`. **Other Alternatives** Before the spread syntax was introduced, developers might have used other methods to achieve similar results, such as: * Using the `concat()` method: `var arr = [1, 2, 4].concat([6])` * Creating a new array using the `Array` constructor and pushing the element manually: `var arr = new Array(3).fill(0).map((_, i) => { if (i == 1) return 6; });` These alternatives are less efficient than the spread syntax, but still viable options for older browsers or versions that don't support ES6. In summary, the benchmark measures the performance difference between two methods for pushing an element onto an array in JavaScript: `push()` and spread syntax. The spread syntax is generally faster and more concise, but requires ECMAScript 2015 or later support.
Related benchmarks:
Pushing items via Array.push vs. Spread Operator
Spread or Push
spread vs push - simple2
spread vs push - simple3
Spread Operator vs Push Method
Comments
Confirm delete:
Do you really want to delete benchmark?