Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test concat vs spread+push
(version: 0)
Comparing performance of:
concat vs spread+push
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var original = [1,2,3,4,5,6,7,8,9,10]; var _new = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
Tests:
concat
original = original.concat(_new);
spread+push
original.push(..._new);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
concat
spread+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 two methods for combining arrays in JavaScript: `concat` and the spread operator (`...`) with `push`. **Options Compared:** * **`concat`:** This method creates a new array by appending all elements of the second array to the first. * **Pros:** Well-established, widely understood syntax. Works for combining arrays of any data type. * **Cons:** Can be slower than the spread operator + `push` because it creates a completely new array. * **`spread+push`:** This method uses the spread operator to expand the second array into individual elements, then uses `push` to add those elements to the first array. * **Pros:** Potentially faster because it modifies the original array in-place rather than creating a new one. * **Cons:** Less familiar syntax for some developers. Can be less efficient if modifying large arrays frequently, as `push` can have performance overhead. **Alternatives:** * **`.map()` + concatenation:** This approach maps over elements of the first array and concatenates new values with the second array's elements. It offers more flexibility but might be slower than the other options. * **Libraries like Lodash:** Libraries often provide optimized versions of common operations, including array combination. While convenient, they introduce a dependency. **Other Considerations:** * **Array Size:** The performance difference between `concat` and `spread+push` is more noticeable with larger arrays. * **Use Case:** If immutability is crucial, `concat` might be preferred as it always returns a new array. For frequent modifications, `spread+push` could be faster. Let me know if you have any other questions about this benchmark or JavaScript array manipulation techniques!
Related benchmarks:
Array.prototype.concat vs spread operator with more numbers
Array concat vs spread operator vs push v2
concat vs spread three arrays
concat vs spread vs push three arrays to new one FIX
Comments
Confirm delete:
Do you really want to delete benchmark?