var params = [ 3 ];
var other = [ 1, 2 ].concat(params);
var params = [ 3 ]
var other = [ 1, 2, params ]
var other = [ 1, 2 ].push(3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 13688393.0 Ops/sec |
spread operator | 13736609.0 Ops/sec |
Push | 22722558.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare three different approaches for concatenating arrays in JavaScript:
Array.prototype.concat()
...
)push()
methodOptions Compared
Array.prototype.concat()
: This is the traditional way of concatenating arrays using the concat()
method....
): Introduced in ES6, this operator allows you to expand an array into a new array by including all elements from the original array.push()
method: In this approach, each element is pushed onto the end of the existing array using the push()
method.Pros and Cons
Array.prototype.concat()
:...
):push()
method:Library Usage
None of the test cases use a specific library. They rely on built-in JavaScript features and standard library functions.
Special JS Features/Syntax
The spread operator (...
) is the only feature that requires ES6 compatibility. This means that browsers older than Firefox 88 (the one running this benchmark) might not support it.
Other Alternatives
For larger arrays or performance-critical code, you might also consider using other methods like:
Array.prototype.set()
: A more modern way of updating an array's length and filling its elements.Array.from()
: Creating a new array from an existing iterable.concat
function.Keep in mind that the choice of method depends on your specific use case, performance requirements, and target browser support.