console.log('Testing array spread vs. array concatenation');
const largeNumber = 1000;
const largeArray = Array(largeNumber).fill('string');
const bigArray = Array(largeNumber).fill(99);
let data;
for (let i = 0; i < largeNumber; i++) {
data = largeArray.concat(bigArray);
}
const largeNumber = 1000;
const largeArray = Array(largeNumber).fill('string');
const bigArray = Array(largeNumber).fill(99);
for (let i = 0; i < largeNumber; i++) {
data = [largeArray, bigArray];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Concatenation | |
Spread |
Test name | Executions per second |
---|---|
Concatenation | 1124.8 Ops/sec |
Spread | 245.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
The provided JSON benchmark measures the performance difference between two approaches to concatenate large arrays in JavaScript:
concat()
method to combine two arrays....
) to create a new array by spreading the elements of an existing array.Options compared
The benchmark compares two options:
concat()
method....
).Pros and Cons of each approach:
concat()
.Library usage
None of the benchmarked code uses any external libraries. The Array()
constructor and the concat()
method are built-in JavaScript functions.
Special JS features or syntax
The benchmark does not use any special JavaScript features or syntax, such as async/await, Promises, or Web Workers.
Other alternatives
If you're interested in exploring alternative approaches to array concatenation, consider the following:
Array.prototype.reduce()
to concatenate arrays.Keep in mind that these alternatives may have varying performance characteristics and may not be supported by all browsers.