var array = new Array(10).fill(1)
var toConcat = new Array(10).fill(2)
var other = array.concat(toConcat);
var other = [ array, toConcat ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 6169227.5 Ops/sec |
spread operator | 8347683.0 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Definition
The benchmark is designed to compare two approaches for concatenating arrays in JavaScript:
Array.prototype.concat()
...
)The benchmark tests these two approaches on an array of length 10, filled with 1s (var array = new Array(10).fill(1)
), and another array of the same length filled with 2s (var toConcat = new Array(10).fill(2)
).
Options Compared
Two options are compared:
Array.prototype.concat()
: This method takes an existing array as an argument and returns a new array that contains all elements from both arrays....
): This syntax allows you to create a new array by spreading the elements of another array into it.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Array.prototype.concat()
: Pros:...
): Pros:concat()
Library/External Functionality
There is no external library or function used in this benchmark.
Special JavaScript Feature/Syntax
The spread operator (...
) is a relatively recent addition to the JavaScript language, introduced in ECMAScript 2015 (ES6). It's designed to simplify the process of creating new arrays by spreading elements from an existing array.
Other Alternatives
If you're interested in exploring alternative approaches for concatenating arrays, here are some other options:
Array.prototype.push()
: Instead of using concat()
or spread operator, you could use push()
to add elements to the end of the array. However, this approach would require modifying the original array and might not be as efficient.for...of
loop: You could also use a for...of
loop to iterate over the elements of one array and push them into another.Keep in mind that these alternatives may have different performance characteristics or requirements compared to concat()
and spread operator.