const result = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10].concat([1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]);
const result = [ [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]]
const result = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10].push( [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 6021201.5 Ops/sec |
spread operator | 9052250.0 Ops/sec |
Push | 7951601.5 Ops/sec |
Let's dive into explaining the provided benchmark.
Benchmark Overview
The benchmark is designed to compare the performance of three different ways to concatenate arrays in JavaScript: concat()
, the spread operator (...
), and push()
.
Options Compared
...
): This is a new feature introduced in ECMAScript 2018 (ES9). It allows you to expand an array into individual elements, effectively creating a new array.Pros and Cons of Each Approach
...
):push()
works.Library and Special JS Features
None mentioned in the provided benchmark.
Other Considerations
When choosing between these approaches, consider the following:
push()
.concat()
is generally more concise and readable than push()
.Alternatives
If you're interested in exploring other options, consider:
slice()
to create a shallow copy of the array and then concatenates it with another array.for...of
loop to iterate over an array and concatenate its elements.Array.from()
to create an array from an iterable.Keep in mind that these alternatives might have slightly different performance characteristics or be more prone to errors than the original options.