// Used to popolate the arrays to merge with 10.000.000 elements.
let array1 = [];
let array2 = [];
for (let i = 0; i < 10000000; ++i) {
array1.push(i);
array2.push(i);
}
const array3 = [ array1, array2 ];
// Used to popolate the arrays to merge with 10.000.000 elements.
let array1 = [];
let array2 = [];
for (let i = 0; i < 10000000; ++i) {
array1.push(i);
array2.push(i);
}
// The concat performance test.
const array3 = array1.concat(array2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
concat |
Test name | Executions per second |
---|---|
spread | 2.7 Ops/sec |
concat | 2.8 Ops/sec |
Measuring performance of JavaScript microbenchmarks can be complex, and the results might vary depending on several factors such as hardware, software environment, system load, etc.
What is being tested?
The provided JSON represents two benchmark definitions:
...
) to merge two arrays of 10 million elements each. The code creates two empty arrays and then populates them with numbers from 0 to 9,999,999 using a for
loop. Finally, it merges the two arrays together using the spread operator.concat()
method to merge two arrays of 10 million elements each.Options compared
In both cases, the options being compared are:
...
)concat()
)These two approaches have different performance characteristics due to how they handle array merging and memory allocation.
Pros and Cons:
...
):concat()
):Library usage
None of the provided benchmark definitions use any external libraries or frameworks.
Special JS features or syntax
The spread operator (...
) is a modern JavaScript feature introduced in ECMAScript 2015. It allows for creating new arrays by spreading elements from an existing array into a new one.
Other alternatives
If you were to compare these two approaches with other methods, you might consider:
array1.slice(0).concat(array2)
).push()
method on an empty array to add elements from another array.Keep in mind that these alternatives may have different trade-offs and may not be suitable for all scenarios.