var params = [ "hello", true, 7 ];
var clone = [ params ];
clone.push({something: 'new'});
var params = [ "hello", true, 7 ]
var clone = params.concat([{something: 'new'}])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 20655246.0 Ops/sec |
spread operator | 5898549.0 Ops/sec |
Let's break down the benchmark and its test cases.
What is being tested?
The provided JSON represents two individual test cases that compare the performance of two approaches for creating a new array:
Array.prototype.concat()
: This method creates a new array by concatenating an existing array with another array or array-like object.[ ...params ]
): This syntax creates a new array by spreading the elements of an existing array.Options compared
The two test cases are comparing the performance of these two approaches for creating a new array:
Array.prototype.concat()
[ ...params ]
)Pros and Cons of each approach:
Array.prototype.concat()
concat()
callback function to modify the new array.[ ...params ]
)Special JS feature/syntax:
The spread operator ([ ...params ]
) is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It's part of the new syntax features that allow for more concise and expressive array creation.
Library usage:
There are no libraries used in these test cases. The benchmark tests only the native JavaScript implementations of Array.prototype.concat()
and the spread operator.
Other considerations:
When evaluating performance, it's essential to consider factors such as:
Alternatives:
For creating a new array, other methods can be used, such as:
Array.from()
: Creates a new array from an iterable or array-like object.Array.prototype.push()
with an initial value: Creates a new array by pushing elements onto the existing array.However, these alternatives may have different performance characteristics compared to the spread operator and concat()
, which is why this benchmark specifically compares those two approaches.