const test = [].concatArray(10000).fill('1').concat(Array(100).fill('2'));
const test = [ Array(10000).fill('1'), Array(100).fill('2')];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 0.0 Ops/sec |
Spread operator | 6987.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark measures the performance difference between two approaches for concatenating arrays in JavaScript: Array.prototype.concat()
and the new ES6 spread operator (...
).
Options compared:
...
): This operator creates a new array by iterating over an iterable (such as an array) and creating a new element for each iteration.Pros and Cons of each approach:
concat()
), which can add overhead....
):Array.prototype.concat()
.Library:
None.
Special JavaScript features or syntax:
The benchmark uses ES6 spread syntax (...
), which is a relatively new feature introduced in ECMAScript 2015. The spread operator allows you to create a new array by iterating over an iterable and creating a new element for each iteration.
Other alternatives:
push()
to add elements to the end of one array.reduce()
to concatenate two arrays by accumulating the results in an accumulator variable.+
operator or string literals.Keep in mind that these alternatives may not be as efficient or concise as the spread operator or Array.prototype.concat()
, depending on your specific use case and performance requirements.