var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 5215034.0 Ops/sec |
spread operator | 36878792.0 Ops/sec |
Benchmark Explanation
MeasureThat.net is testing the performance difference between two ways to concatenate arrays in JavaScript: using the concat()
method and the spread operator (...
).
The benchmark compares these two approaches on an example dataset containing a string, a boolean, and an integer. The goal is to determine which approach is faster.
Options Comparison
There are two options being compared:
...
): This operator allows you to expand an array into individual elements, which can then be used in a function call.Pros and Cons of Each Approach
...
):concat()
due to reduced overheadLibrary Consideration
None of the benchmark test cases use any external libraries. The JavaScript core functions (Array.prototype.concat
and the spread operator) are being tested.
Special JS Features/Syntax
The benchmark uses the ES6 spread operator (...
). This feature was introduced in ECMAScript 2015 (ES6) and allows for more concise array expansion.
Other Alternatives
Alternative approaches to concatenate arrays could include:
Array.prototype.push()
with an array as an argumentArray.prototype.set()
(although this is not a standard method and may not be supported in all browsers)Array
constructor and iterating over the elements from the original arraysKeep in mind that these alternatives might have different performance characteristics or syntax requirements compared to the spread operator.
Benchmark Preparation Code
The provided code snippet is a minimal example, likely created for benchmarking purposes:
var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
And similarly for the spread operator test case:
var params = [ "hello", true, 7 ]
var other = [ 1, 2, ...params ];
These examples illustrate how to concatenate arrays using the two tested approaches.