var params = new Array(10000);
var other = [ 1, 2 ].concat(params);
var params = new Array(10000);
var other = [ 1, 2 ]
Array.prototype.push.apply(params, other)
var params = new Array(10000);
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 | 810129.4 Ops/sec |
spread operator | 683898.2 Ops/sec |
Push | 4026.9 Ops/sec |
Let's break down the provided benchmark and its test cases.
What is being tested?
The benchmark compares three different approaches to concatenate arrays:
Array.prototype.concat()
: This method takes an array and another array as arguments, and returns a new array containing all elements from both arrays....
): Introduced in ES6, this operator allows you to expand an iterable (like an array) into individual elements that can be used inside the expression.Array.prototype.push()
: This method adds one or more elements to the end of the current array.Options compared
The benchmark is comparing the performance of these three approaches on an array with 10,000 elements:
Array.prototype.concat()
...
)Array.prototype.push()
Pros and Cons
Here are some general pros and cons of each approach:
Array.prototype.concat()
:...
):concat()
because it only requires passing the individual elements of one array to the push()
method, rather than copying all elements from both arrays. Also, it's more concise and modern syntax.Array.prototype.push()
:concat()
or the spread operator, especially when dealing with larger arrays.Library usage
None of these test cases use any external libraries. They rely solely on built-in JavaScript functionality for their operations.
Special JS feature/syntax
The spread operator (...
) is a new syntax introduced in ES6, which allows you to expand an iterable into individual elements. It's not supported in older browsers or environments that don't have modern JavaScript engines.
Other alternatives
Some other approaches to concatenate arrays include:
Array.prototype.splice()
: While this method can be used for array concatenation, it modifies the original array and has additional overhead compared to push()
.