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 | 3415450.2 Ops/sec |
spread operator | 20488774.0 Ops/sec |
Push | 12519555.0 Ops/sec |
Let's break down the benchmark and its results.
What is being tested?
The benchmark is testing three different ways to concatenate (add) arrays in JavaScript:
Array.prototype.concat()
: The traditional method of concatenating arrays using the concat()
function....
): The new ES6 spread operator, introduced in 2015, which allows you to expand an array into its elements.push()
with spread syntax ([ ...params ]
inside push()
): A more recent approach that uses the spread operator inside the push()
function.Options compared
The benchmark is comparing these three approaches for concatenating arrays. The options are:
concat()
...
)push()
with spread syntaxPros and Cons of each approach
concat()
:concat()
in a loop....
):concat()
, reducing the chance of array duplication.push()
with spread syntax:push()
with spread syntax.Library and special JS features
There are no libraries or special JavaScript features used in this benchmark, except for:
...
), which is a new feature introduced in ECMAScript 2015.Other alternatives
If you want to consider other approaches, here are some additional options that were not tested in this benchmark:
Array.prototype.push.apply()
: A function call-based approach to concatenating arrays.Array.prototype.reduce()
and reduceRight()
: Array methods used for reducing an array into a single value, which can be used with concatenation.Keep in mind that these alternatives might have different performance characteristics or use cases compared to the approaches tested in this benchmark.