let params = [ "hello", true, 7 ];
params.concat([ 1, 2 ]);
let params = [ "hello", true, 7 ]
params = [ params, 1, 2 ]
var params = [ "hello", true, 7 ];
params.push( [ 1, 2 ]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 7952336.5 Ops/sec |
spread operator | 26247218.0 Ops/sec |
Push | 24669426.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is designed to compare three approaches for concatenating arrays in JavaScript:
Array.prototype.concat()
...
)push()
method with reassigning the array (params.push(...[ 1, 2 ])
)These approaches are being compared because they have different performance characteristics and use cases.
Pros and Cons of Each Approach
Array.prototype.concat()
:...
):push()
method with reassigning the array:concat()
is not supported.Library Usage
None of these approaches rely on external libraries. They are all built-in JavaScript features or methods.
Special JS Features/Syntax
The benchmark uses the modern spread operator (...
) syntax, which is supported in ECMAScript 2015 and later versions. If you're using an older version of JavaScript, this syntax might not work for you.
Other Alternatives
If you need to concatenate arrays but want to avoid these specific approaches:
Array.prototype.reduce()
method: params = [ ...params, 1, 2 ].reduce((acc, val) => [...acc, val], [])
Keep in mind that for most use cases, the spread operator is now the recommended way to concatenate arrays. However, it's essential to consider your target audience, browsers, or environments when choosing an approach.