var array1 = Array(400).fill().map(() => Math.round(Math.random() * 40));
var array2 = Array(400).fill().map(() => Math.round(Math.random() * 40));
var array3 = Array(400).fill().map(() => Math.round(Math.random() * 40));
var array4 = Array(400).fill().map(() => Math.round(Math.random() * 40));
var array5 = Array(400).fill().map(() => Math.round(Math.random() * 40));
var others = array1.concat(array2, array3, array4, array5);
var others = Array.prototype.concat.apply([], [array1, array2, array3, array4, array5]);
var others = [].concat.apply([], [array1, array2, array3, array4, array5]);
var others = [].concat.apply(array1, [array2, array3, array4, array5]);
var others = array1.concat.apply(array1, [array2, array3, array4, array5]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.concat | |
Array.prototype.concat.apply | |
Array.prototype.concat.apply short form | |
Array.prototype.concat.apply short form without new empty array instance in apply | |
Array.prototype.concat.apply short form without new empty array at all |
Test name | Executions per second |
---|---|
Array.concat | 2282107.8 Ops/sec |
Array.prototype.concat.apply | 2223793.2 Ops/sec |
Array.prototype.concat.apply short form | 2242323.2 Ops/sec |
Array.prototype.concat.apply short form without new empty array instance in apply | 2260469.0 Ops/sec |
Array.prototype.concat.apply short form without new empty array at all | 2358585.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition: The benchmark tests the performance of different approaches for concatenating multiple arrays in JavaScript. Specifically, it compares:
Array.concat()
with multiple argumentsArray.prototype.concat.apply()
with an empty array as the first argumentArray.prototype.concat.apply()
without creating a new empty array instanceArray.prototype.concat.apply()
without any arguments (i.e., just applying the arrays directly)Options Compared:
array1.concat(array2, array3, array4, array5)
Array.prototype.concat.apply()
with an empty array: Array.prototype.concat.apply([], [array1, array2, array3, array4, array5])
apply
method.Array.prototype.concat.apply()
without new empty array instance: Array.prototype.concat.apply(array1, [array2, array3, array4, array5])
Array.prototype.concat.apply()
without arguments: Array.prototype.concat.apply(array1, [array2, array3, array4, array5])
Library Used: None of the test cases use any external libraries.
Special JS Feature/Syntax: There are no special JavaScript features or syntaxes being tested in this benchmark.
Other Alternatives:
For concatenating arrays, other alternatives could be using Array.prototype.reduce()
or a library like Lodash's concat
function. However, these approaches may not be suitable for all use cases and may have different performance characteristics compared to the methods tested on MeasureThat.net.
It's worth noting that the benchmark is focused on understanding the trade-offs between different array concatenation approaches in JavaScript. The results can help developers make informed decisions about which approach to use depending on their specific requirements and performance constraints.