var params = new Array(20);
var other = [ 1, 2 ].concat(params);
var params = new Array(20);
var other = [ 1, 2, params ]
var params = new Array(20);
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 | 1321332.8 Ops/sec |
spread operator | 1863877.6 Ops/sec |
Push | 3165718.5 Ops/sec |
Let's break down what is being tested in this benchmark.
The test case compares three approaches to concatenate an array: the traditional concat()
method, the spread operator (...
), and the push()
method.
Options compared:
params
) and returns it....
): This operator spreads the elements of an iterable (such as an array) into individual arguments to be passed to a function.push()
is used in combination with the spread operator to concatenate the arrays.Pros and cons of each approach:
...
):push()
with the simplicity of the spread operator.The benchmark results show that the order of operations and syntax used can impact performance. The push approach is the fastest, followed closely by the spread operator, while the concat method is slower.
Library and purpose:
There are no external libraries mentioned in this benchmark. However, some browsers may use polyfills or built-in functions to support the spread operator and other modern JavaScript features.
Special JS feature or syntax:
The spread operator (...
) was introduced in ECMAScript 2015 (ES6) as a new way to create arrays from iterables. It is also known as "rest parameter" syntax.
In this benchmark, the test case uses the spread operator to concatenate arrays, and it's interesting to see how different browsers and versions perform with this approach.
Other alternatives:
If you want to explore alternative approaches, here are a few more options:
Buffer.concat()
.Keep in mind that these alternatives may have different performance characteristics and use cases compared to the approaches tested in this benchmark.