var params = [ "hello", true, 7 ];
var other = [ 1, 2 ]
other = other.concat(params);
var params = [ "hello", true, 7 ];
var other = [ 1, 2 ]
other = other.push(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2 ]
var other = [ other, params ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Push | |
New array spread |
Test name | Executions per second |
---|---|
Array.prototype.concat | 7132648.0 Ops/sec |
Push | 30568930.0 Ops/sec |
New array spread | 26185424.0 Ops/sec |
Let's dive into the explanation.
Benchmark Description
The benchmark is called "Array concat vs spread operator vs push vs new array spread". It compares four different ways to concatenate two arrays in JavaScript: concat()
, spread operator (...
), push()
with rest parameter, and creating a new array using the spread operator.
Test Cases
There are three test cases:
concat()
method to merge two arrays.other = other.concat(params);
concat()
method.push()
method with rest parameter to add elements from one array to another.other = other.push(...params);
push()
method with rest parameter to add elements from one array to another....
) to create a new array by spreading the contents of two arrays.var other = [ ...other, ...params ];
...
)Benchmark Results
The latest benchmark results show the execution speed (Executions Per Second) for each test case on Chrome 89:
Pros and Cons
concat()
method:...
):push()
with rest parameter:Other Considerations
...
) for performance benefits.concat()
or push()
methods.Alternatives
If you don't want to use the spread operator or any of the above methods, you could consider:
Array.prototype.set()
(ES2022) to update an array in place.