const arr1 = [];
const arr2 = [];
const arr3 = [];
const arrs = []
for (let i = 0; i < 2000; i++) {
arr1.push(i);
}
for (let i = 0; i < 3000; i++) {
arr2.push(i);
}
for (let i = 0; i < 1000; i++) {
arr3.push(i);
}
arrs.push(arr1, arr2, arr3);
arrs.reduce((memo, val) => {
return memo.concat(val);
}, []);
const arr1 = [];
const arr2 = [];
const arr3 = [];
const arrs = []
for (let i = 0; i < 2000; i++) {
arr1.push(i);
}
for (let i = 0; i < 3000; i++) {
arr2.push(i);
}
for (let i = 0; i < 1000; i++) {
arr3.push(i);
}
arrs.push(arr1, arr2, arr3);
arrs.reduce((memo, val) => {
return [memo, val];
}, []);
const arr1 = [];
const arr2 = [];
const arr3 = [];
const arrs = []
for (let i = 0; i < 2000; i++) {
arr1.push(i);
}
for (let i = 0; i < 3000; i++) {
arr2.push(i);
}
for (let i = 0; i < 1000; i++) {
arr3.push(i);
}
arrs.push(arr1, arr2, arr3);
arrs.reduce((memo, val) => {
for (const value of val) {
memo.push(val);
}
return memo;
}, []);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 18139.8 Ops/sec |
spread operator | 9228.8 Ops/sec |
Push | 11166.6 Ops/sec |
I'll break down the benchmark and its various components.
Benchmark Overview
The "Fork - Array concat vs spread operator vs push v2" benchmark compares the performance of three different methods for concatenating arrays in JavaScript:
Array.prototype.concat()
...
)push()
method with a loopOptions Compared
Here's a brief overview of each option:
Array.prototype.concat()
: This is the traditional way to concatenate arrays in JavaScript. It takes two or more arrays as arguments and returns a new array containing all elements from the input arrays....
): Introduced in ES6, this operator allows you to create a new array by spreading an existing array (or other iterable) into multiple elements.push()
method with a loop: This approach uses a traditional for
loop to iterate over the elements of one or more arrays and pushes them onto another array using the push()
method.Pros and Cons
Here's a brief summary of each option:
Array.prototype.concat()
:...
):push()
method with a loop:concat()
is not available.Library and Syntax
None of the benchmark tests use any external libraries. However, they do utilize some advanced JavaScript syntax:
...
) is used in the "spread operator" test case.for
loop with a variable declaration (i.e., let i = 0;
) is used in all three test cases.Special JS Features
No special JS features are required or mentioned in the benchmark. All test cases use standard JavaScript syntax and methods.
Other Alternatives
If you're interested in exploring alternative approaches to array concatenation, here are a few options:
Array.prototype.reduce()
with an accumulator: This method is more concise than a traditional loop but can be slower for large arrays.Array.prototype.map()
followed by Array.prototype.concat()
: This approach creates multiple intermediate arrays, which may not be suitable for large datasets.Keep in mind that the performance differences between these alternatives are usually negligible unless you're working with extremely large datasets or performance-critical code.