Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Add a lot of elements to array: push vs destructuring
(version: 0)
Comparing performance of:
spread operator vs Push
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
spread operator
const before = Array(10000).fill('before') const after = Array(10000).fill('after') const other = [ ...before, ...after ]
Push
const before = Array(10000).fill('before') const after = Array(10000).fill('after') before.push(...after)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread operator
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread operator
18284.7 Ops/sec
Push
23884.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test and explain what's being tested. **Benchmark Test:** The benchmark test is designed to compare two approaches for adding a large number of elements to an array in JavaScript: 1. **Spread Operator (Spread Syntax)**: This approach uses the spread operator (`...`) to concatenate two arrays. 2. **Push Method**: This approach uses the `push()` method to add one or more elements to the end of an array. **What's Being Tested:** The benchmark test is measuring the performance difference between these two approaches when adding 10,000 elements to an array. **Options Compared:** The two options being compared are: 1. **Spread Operator (Spread Syntax)**: This approach creates a new array by copying all elements from `before` and `after` arrays using the spread operator. 2. **Push Method**: This approach adds all elements from `after` array to the end of `before` array using the `push()` method. **Pros and Cons of Each Approach:** 1. **Spread Operator (Spread Syntax)**: * Pros: + More concise and expressive code + Avoids modifying original arrays * Cons: + Can be slower due to the overhead of creating a new array + May incur additional memory allocation 2. **Push Method**: * Pros: + Faster execution time, as it modifies the existing array in-place + Less memory allocation required * Cons: + More verbose code + May modify original arrays **Library Used:** None explicitly mentioned in the benchmark definition or test cases. However, the use of `Array.prototype.fill()` and `Array.prototype.push()` suggests that the underlying JavaScript engine is being tested. **Special JS Feature/Syntax:** The spread operator (`...`) is a relatively new feature introduced in ECMAScript 2015 (ES6). It allows for concise array concatenation and spreading elements into objects. This syntax is widely supported across modern browsers and Node.js engines. **Other Alternatives:** If you need to add elements to an array, other alternatives could be: 1. **Array.prototype.concat()**: Similar to the spread operator, but returns a new array instead of in-place modification. 2. **Array.prototype.slice() + Array.prototype.push()**: Creates a shallow copy of `before` using `slice()`, then appends elements from `after` using `push()`. Keep in mind that these alternatives might have slightly different performance characteristics compared to the spread operator and push method, but they can be useful depending on your specific use case.
Related benchmarks:
Array construct vs array push
Pushing items via Array.push vs. Spread Operator
Splice vs Spread vs Unshift vs Push to insert at beginning of array
Add item to array: push vs spread vs assign vs assign+grow
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?