Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push larger list
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
spread operator vs Push
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
spread operator
var params = Array(2000000).fill(2); var other = [ ...params, 1 ]
Push
var params = Array(2000000).fill(2); var other = params.push(1);
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to compare three approaches for concatenating an array: 1. **Push**: Using the `push()` method to add elements to the end of the array. 2. **Spread Operator (`...`)**: Utilizing the new ES6 spread operator to create a new array by copying and extending the original array. **Options Compared:** The benchmark compares the performance of these two methods (Push and Spread Operator) for concatenating an array with a large size (2000000 elements, filled with 2). The Push method involves calling `push()` on the end of the array to add new elements, while the Spread Operator creates a new array by copying the original array's elements and adding a single element (1). **Pros and Cons:** * **Push Method:** + Pros: - Simple and widely supported. - Can be useful for modifying existing arrays. + Cons: - Can lead to performance issues when dealing with large arrays, as it requires multiple array allocations and resizing. * **Spread Operator (`...`):** + Pros: - More concise and readable than the Push method. - Creates a new array without modifying the original one. + Cons: - May incur overhead due to creating a new array copy. **Library and Special JS Features:** The benchmark uses no external libraries. However, it does utilize a JavaScript feature introduced in ECMAScript 2018 (ES2018): the spread operator (`...`). The spread operator allows for concise array creation by expanding an existing array or object into a new one. **Other Considerations:** * **Array Allocation:** When using the Push method, the browser needs to allocate new memory for each added element. This can lead to increased memory usage and slower performance. * **Cache Locality:** Modern browsers use various caching mechanisms to improve performance. However, when concatenating arrays using the Push method, the cache locality is disrupted due to the frequent allocation of new memory. **Alternatives:** In addition to the two methods being compared in this benchmark, other approaches for array concatenation exist: * **`concat()` Method:** This is another widely supported method for concatenating arrays. While it may have similar performance characteristics to the Push method, its syntax can be less readable. * **`array.prototype.reduce()` Method:** Some browsers support using `reduce()` to concatenate arrays. However, this approach often requires more code and can lead to less cache locality. For a simple array concatenation task, the Spread Operator (`...`) is generally recommended due to its concise syntax and performance benefits.
Related benchmarks:
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
Array concat vs spread operator vs push #3
Array.prototype.concat vs spread operator real
Array.prototype.concat vs spread operator on large array
Comments
Confirm delete:
Do you really want to delete benchmark?