var params = new Array(300);
var other = [ 1, 2 ].concat(params);
var params = new Array(300);
var other = [ 1, 2, params ]
var params = new Array(300);
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 | 549107.0 Ops/sec |
spread operator | 104348.1 Ops/sec |
Push | 69595.7 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The benchmark is designed to compare three different approaches for concatenating or spreading an array of 300 elements:
Array.prototype.concat()
...
)Array.prototype.push()
with the spread operator (...
)Options Compared
concat()
: a traditional method for concatenating arrays, which creates a new array by appending elements from one or more source arrays....
): a new syntax introduced in ES6 that allows you to expand an iterable (like an array) into individual elements, effectively spreading them out into a new array. In the context of concat()
, it's used to spread the elements of the original array into the new array.push()
with spread operator (...
): a variation of the traditional push()
method that uses the spread operator to append elements from an iterable (like an array) to the end of the current array.Pros and Cons
Here's a brief summary:
...
):push()
with spread operator (...
):Library
None of the test cases use any external libraries.
Special JS Features or Syntax
The benchmark uses the new ES6 spread operator (...
), which was introduced in ECMAScript 2015. This syntax allows you to expand an iterable into individual elements, making it a concise and efficient way to concatenate arrays.
Other Alternatives
If these approaches are not suitable for your use case, some alternative methods include:
Array.prototype.slice()
or Array.prototype.splice()
to create a shallow copy of the original array.for
loop or a forEach()
method to iterate over the elements and append them manually.Keep in mind that the choice of method depends on your specific requirements, such as performance, readability, and compatibility.