var params = [ "hello", true, 7 ];
params = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
var params = [ "hello", true, 7 ]
params = params.push(params)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
push with spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 14152422.0 Ops/sec |
spread operator | 54931880.0 Ops/sec |
push with spread operator | 69313392.0 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares the performance of three different approaches to concatenate an array: Array.prototype.concat
, the spread operator (...
), and push
with the spread operator.
Options Comparison
Array.prototype.concat
: This method creates a new array by concatenating the elements of two arrays....
): This operator allows you to expand an array into separate arguments.concat
.push
with spread operator: This approach uses the push
method to add elements to an existing array, followed by the spread operator to expand the array into separate arguments.Library: None
There are no external libraries used in this benchmark.
Special JS Features/Syntax: Spread Operator (...
)
The spread operator is a new feature introduced in ECMAScript 2015 (ES6). It allows you to expand an array or object into separate arguments. In the benchmark, it's used to concatenate the params
array with another array using the syntax var other = [ 1, 2, ...params ]
.
Alternative Approaches
Other approaches to concatenating arrays in JavaScript might include:
However, these alternatives are not included in this benchmark.