var array1 = [];
var array2 = [];
var array3 = [];
for (let m = 0; m < 10; m++) {
array1.push(m);
array2.push(m + 10);
array3.push(m + 20);
}
let array = [array1, array2, array3];
let array = array1.slice();
for (let i = 0; i < array2.length; i++) {
array.push(array2[i]);
}
for (let i = 0; i < array3.length; i++) {
array.push(array3[i]);
}
let array = array1.slice();
array.push(array2);
array.push(array3);
let array = array1.slice();
array.push(array2, array3);
let array = [];
array.push(array1, array2, array3);
let array = [].concat(array1, array2, array3);
let array = [].concat(array1).concat(array2).concat(array3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using array spread | |
Using clone and push loop | |
Using clone and push spread | |
Using clone and push spread 2 | |
Using push spread | |
Using array concat | |
Using array concat (2) |
Test name | Executions per second |
---|---|
Using array spread | 5324139.0 Ops/sec |
Using clone and push loop | 10082558.0 Ops/sec |
Using clone and push spread | 4393178.5 Ops/sec |
Using clone and push spread 2 | 3553577.8 Ops/sec |
Using push spread | 2945258.8 Ops/sec |
Using array concat | 7670045.0 Ops/sec |
Using array concat (2) | 3492119.2 Ops/sec |
Let's dive into the benchmarking test cases and explanations.
Benchmark Definition:
The benchmark definition is simple: merging three small arrays of 10 elements using different approaches. The script preparation code creates three empty arrays (array1
, array2
, and array3
) and populates them with numbers from 0 to 9, respectively, in separate loops. This setup ensures that the test cases only depend on the order and size of the input arrays, making it easier to compare different approaches.
Options compared:
The benchmark compares six different approaches for merging the three arrays:
array = [...array1, ...array2, ...array3];
): This approach uses the spread operator (...
) to concatenate the arrays.let array = array1.slice(); for (let i = 0; i < array2.length; i++) { array.push(array2[i]); } for (let i = 0; i < array3.length; i++) { array.push(array3[i]); };
): This approach creates a clone of the first array using array1.slice()
and then pushes each element from the other two arrays onto it.let array = array1.slice(); array.push(...array2); array.push(...array3);
): Similar to the previous one, but uses the spread operator for pushing elements.let array = array1.slice(); array.push(...array2, ...array3);
): This approach combines the clone of array1
with both array2
and array3
using multiple spread operators.let array = []; array.push(...array1, ...array2, ...array3);
): Similar to the previous one, but initializes an empty array and pushes all elements onto it using the spread operator.let array = [].concat(array1, array2, array3);
): This approach uses the concat()
method to concatenate the arrays.Pros and Cons:
Here's a brief overview of each approach:
slice()
and loops.concat()
methods.Libraries and special features:
No external libraries are used in this benchmark, but some modern JavaScript features like const
and arrow functions (=>
) are implicitly used due to the nature of the code.
Other considerations:
In conclusion, the benchmark helps highlight the trade-offs between different approaches for merging arrays. By considering both pros and cons, developers can choose the most suitable method for their specific use cases.