var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].push(params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 7993637.0 Ops/sec |
spread operator | 35844440.0 Ops/sec |
Push | 52250540.0 Ops/sec |
Benchmark Overview
The provided benchmark, "concat vs spread operator vs push", compares the performance of three different methods for concatenating arrays in JavaScript: the traditional concat()
method, the new ES6 spread operator (...
), and the push()
method.
Test Cases
There are three individual test cases:
concat()
method to concatenate an array with a variable-length array (params
) containing strings, booleans, and numbers....
) to concatenate an array with a variable-length array (params
) containing strings, booleans, and numbers.push()
method to concatenate an array with a variable-length array (params
) containing strings, booleans, and numbers.Options Compared
The benchmark compares the performance of three different approaches:
concat()
: The traditional method for concatenating arrays in JavaScript. It creates a new array by copying elements from both input arrays....
): A new feature introduced in ES6 that allows concatenation by spreading elements into a new array.push()
: The push()
method modifies the existing array and appends elements to it.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
concat()
:...
):concat()
, creates a new array in a more efficient way (by using a single array literal).push()
:Library Usage
None of the test cases use a library, as they are purely JavaScript methods implemented by the browser engine.
Special JS Features or Syntax
The spread operator (...
) is a special feature introduced in ES6. It's not supported in older browsers, which explains why Chrome 98 has significantly faster execution times for this benchmark compared to lower versions of Chrome.
Other Alternatives
For concatenating arrays, other alternatives could include:
Array.prototype.reduce()
: Can be used to concatenate arrays by reducing them into a single array.Array.prototype.flat()
: May be used in the future as an alternative for concatenation.Overall, the benchmark highlights the importance of considering performance when choosing methods for array concatenation in JavaScript. The spread operator has become a popular choice due to its conciseness and efficiency, while concat()
remains widely supported but less efficient.