Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push (to end)
(version: 0)
Compare the spread operator with the traditional concat() method and push
Comparing performance of:
Array.prototype.concat vs spread operator vs Push
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
const params = [1,2,3,4,5,6,7,8,9,10]; var other = params.concat(11);
spread operator
const params = [1,2,3,4,5,6,7,8,9,10]; var other = [...params, 11]
Push
const params = [1,2,3,4,5,6,7,8,9,10]; var other = params.push(11);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
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
gemma2:9b
, generated one year ago):
This benchmark compares three different ways to add the number `11` to the end of an array: * **`Array.prototype.concat()`**: This is the traditional method for combining arrays. It creates a new array containing all elements from the original array and the added element. * **Pros:** Well-established, works reliably. * **Cons:** Can be slower than other methods, especially for large arrays because it creates a new array. * **Spread Operator (`...`)**: This modern syntax expands an iterable (like an array) into its individual elements. * **Pros:** Concise and often faster than `concat()` as it avoids creating a new array in many cases. * **Cons:** Newer syntax, may not be supported by all JavaScript environments. * **`Array.prototype.push()`**: This method adds an element to the end of an existing array and returns the new length of the array. * **Pros:** Modifies the original array directly, efficient for adding a single element. * **Cons:** Doesn't return a new array, which might be desirable in some cases. **Other Considerations:** * **Performance Impact**: The benchmark results show that `push()` is generally the fastest, followed by the spread operator, and then `concat()`. * **Code Readability**: The spread operator often leads to more concise and readable code. * **Memory Usage**: Creating new arrays with `concat()` can lead to higher memory consumption if you're working with large arrays. **Alternatives:** While these are the three primary methods for appending elements to an array in JavaScript, there are other ways to achieve similar results depending on your specific needs: * **Array Methods:** JavaScript provides other useful array methods like `unshift()` (adds elements to the beginning) and `slice()`, which can be helpful for manipulating arrays. * **Object Manipulation**: For certain use cases, it might be more efficient to store data in objects instead of arrays if you frequently need to access elements by key.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array concat vs spread operator vs push with more data
Array concat vs spread operator vs push larger list
Array.prototype.concat vs spread operator vs push with spread
Array concat vs spread operator vs push + spread 2023-08-21
Comments
Confirm delete:
Do you really want to delete benchmark?