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 allArrays = [array1, array2, array3, array4, array5];
var others = [].concat.apply([], allArrays);
var others = allArrays.flat()
var others = allArrays.reduce(((combinedLength, label) => combinedLength + label.length), 0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.apply | |
array.flat() | |
array.reduce |
Test name | Executions per second |
---|---|
array.apply | 201445.0 Ops/sec |
array.flat() | 25738.5 Ops/sec |
array.reduce | 10768165.0 Ops/sec |
Let's break down what's being tested in this benchmark and explain the different approaches.
Overview
The benchmark is designed to compare the performance of three array manipulation methods:
Array.prototype.concat.apply()
Array.prototype.flat()
Array.prototype.reduce()
These methods are used to combine or manipulate arrays, but they have different underlying algorithms and implementation details that can impact their performance.
Array.prototype.concat.apply()
concat.apply()
is a legacy method that concatenates two or more arrays by creating a new array and copying the elements from each input array. The apply()
method applies the concat()
function to the input arrays, passing the context of the first argument as an additional argument.
In this benchmark, Array.prototype.concat.apply()
is used with the empty
array as the initial value, and then it concatenates all five arrays (array1
, array2
, etc.) using the spread operator ([]
). The apply()
method is used to call the concat()
function on the first argument.
Pros:
Cons:
Array.prototype.flat()
flat()
is a more modern method introduced in ECMAScript 2019. It returns a new array containing all subarrays of the input array.
In this benchmark, Array.prototype.flat()
is used to flatten all five arrays (array1
, array2
, etc.) into a single array using the spread operator ([]
).
Pros:
concat.apply()
, as it avoids creating multiple intermediate arraysCons:
flat()
Array.prototype.reduce()
reduce()
is a method that applies a function to each element of an array, accumulating a result.
In this benchmark, Array.prototype.reduce()
is used to calculate the total length of all arrays (array1
, array2
, etc.) by summing up the lengths of individual elements using the spread operator ([]
).
Pros:
concat.apply()
, as it avoids creating multiple intermediate arraysCons:
Library usage
There is no explicit library being used in this benchmark. However, the Array.prototype
methods are part of the standard JavaScript API.
Special JS features or syntax
None mentioned explicitly, but it's worth noting that the benchmark uses modern ECMAScript features such as arrow functions (() =>
) and template literals ([]
). These features are widely supported by most modern browsers and environments.
Alternatives
Other alternatives for array manipulation could include:
Array.prototype.push()
followed by another loop to concatenate arraysHowever, the benchmark is specifically designed to compare these three standard JavaScript methods, so other alternatives are not being considered.