Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.push vs Spread operator
(version: 0)
Comparing performance of:
Array.push vs Spread operator
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.push
let array = new Array(1000000).fill(0); array.push(1);
Spread operator
let array = new Array(1000000).fill(0); array = [...array, 1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.push
Spread operator
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.push
106.3 Ops/sec
Spread operator
87.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **What is being tested?** The benchmark measures the performance difference between using `Array.push()` and the spread operator (`...`) to add an element to an array of 1 million zeros. The test case has two variants: 1. **Array.push()**: This variant uses the `push()` method to append a single element (1) to the end of the array. 2. **Spread operator**: This variant uses the spread operator (`...`) to create a new array with the original elements and then appends a single element (1) to it. **Options compared** The two options being compared are: 1. **`Array.push()`**: A method that adds one or more elements to the end of an array. 2. **Spread operator (`...`)**: A syntax for creating a new array by spreading elements from an existing array. **Pros and cons of each approach** * **`Array.push()`**: + Pros: Simple, widely supported, and efficient for adding a single element to the end of an array. + Cons: Can be slower than the spread operator for large arrays, as it creates a new internal array structure. * **Spread operator (`...`)**: + Pros: Often faster than `Array.push()` for large arrays, as it avoids creating a new internal array structure. + Cons: Less intuitive and less widely supported than `Array.push()`, especially in older browsers. **Library usage** There is no explicit library mentioned in the benchmark. However, both `Array.push()` and the spread operator are built-in JavaScript methods that do not require any external libraries. **Special JS feature or syntax** None of the test cases use special JavaScript features or syntax beyond what's standard in modern JavaScript implementations. **Other alternatives** If you were to rewrite this benchmark, you could also compare other approaches, such as: 1. **`Array.prototype.concat()`**: A method that creates a new array by concatenating two arrays. 2. **`Array.prototype.set()`**: A method that sets the elements of an array in place, which might be faster for large arrays but is less intuitive than `push()`. 3. **Using `Set` object instead of arrays**: This approach would eliminate the need for arrays altogether and could potentially offer better performance. Keep in mind that these alternatives may not be suitable for all use cases, and the best approach depends on the specific requirements of your project.
Related benchmarks:
Arrays: spread operator vs push
Array: spread operator vs push
Pushing items via Array.push vs. Spread Operator
JS array spread operator vs push
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?