Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.push( spread ) vs assign Array.concat() v2.0
(version: 0)
Comparing performance of:
Array.concat() vs Array.push( spread )
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Array.concat()
var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.concat(params);
Array.push( spread )
var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.push(...params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.concat()
Array.push( spread )
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
meta-externalagent/1.1 (+https://developers.facebook.com/docs/sharing/webmasters/crawler)
Browser/OS:
Other on Other
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.concat()
4954282.5 Ops/sec
Array.push( spread )
24289648.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** This benchmark compares two different ways to merge two arrays in JavaScript: `Array.push(...)` and `Array.concat()`. The goal is to measure which approach is faster. **Test Case 1: Array.concat()** In this test case, we use the `concat()` method to merge two arrays: ```javascript var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.concat(params); ``` Here, we create two separate arrays: `params` and `otherParams`. Then, we use the `concat()` method to merge these two arrays into a new array called `other`. **Test Case 2: Array.push(...)** In this test case, we use the spread operator (`...`) with the `push()` method to merge two arrays: ```javascript var params = [ "hello", true, 7 ]; var otherParams = [ 1, 2 ]; var other = otherParams.push(...params); ``` Here, we create two separate arrays: `params` and `otherParams`. Then, we use the spread operator (`...`) to "spread" the elements of `params` into a single argument for the `push()` method. The `push()` method then appends these elements to `otherParams`, effectively merging the two arrays. **Library/Feature used:** No external libraries are used in this benchmark. However, we do use JavaScript's built-in spread operator (`...`) and array methods (`.concat()` and `.push()`). **Pros/Cons of each approach:** * **Array.concat():** + Pros: - A more traditional and familiar way to merge arrays. - Can handle merging multiple arrays at once. + Cons: - Slower than the spread operator approach (as shown by the benchmark results). - Not as concise or expressive as the spread operator approach. * **Array.push(...):** + Pros: - Faster than `concat()` in this specific scenario (as shown by the benchmark results). - More concise and expressive, making code easier to read. + Cons: - May not be as familiar or intuitive for some developers. - Can only handle merging two arrays at a time. **Other alternatives:** If you need to merge multiple arrays, you can use the `concat()` method with multiple arguments: ```javascript var params = [ "hello", true, 7 ]; var otherParams1 = [ 1, 2 ]; var otherParams2 = [ 3, 4 ]; var other = [...params, ...otherParams1, ...otherParams2]; ``` This approach uses the spread operator to merge multiple arrays into a single array. However, keep in mind that this can become cumbersome and harder to read with many arrays. Alternatively, you can use libraries like Lodash or Underscore.js, which provide more efficient and feature-rich array manipulation functions.
Related benchmarks:
Array concat vs spread operator vs push single element on a 40 elems array
array update push vs spread vs concat
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 with short arrays
Comments
Confirm delete:
Do you really want to delete benchmark?