var arr = Array(100).fill([{n: ''}]);
var params = Array(1000).fill([{n: ''}]);
var other = arr.concat(params);
var params = Array(1000).fill([{n: ''}]);
var other = arr.push(params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 142379.9 Ops/sec |
spread operator | 4803.1 Ops/sec |
The provided JSON represents a JavaScript microbenchmark that compares two approaches for concatenating arrays: the traditional Array.prototype.concat()
method and the new ES6 spread operator (...
).
What is being tested?
In this benchmark, two test cases are compared:
concat()
method to concatenate two arrays....
): This approach uses the spread operator to concatenate two arrays.The benchmark creates an array arr
with 100 elements and fills it with objects, then measures how long it takes to concatenate this array with another large array (1,000 times larger) using each of these approaches.
Options compared
Two options are being compared:
...
): This is a new syntax introduced in ES6 that allows for more concise array concatenation. It works by taking the elements of an iterable (such as an array) and spreading them out into the brackets of another iterable.Pros and Cons
Traditional concat() method
Pros:
Cons:
Spread Operator (...
)
Pros:
Cons:
concat()
methodLibrary used
None. This benchmark uses only built-in JavaScript features.
Special JS feature or syntax
The spread operator (...
) is a new syntax introduced in ES6, which allows for more concise array concatenation.
Other considerations
Alternative approaches
Other alternatives to concatenating arrays include:
Array.prototype.push()
and iterating over an array of elements