var arr = Array(100).fill(Array(20).fill(Math.random()*50))
arr.reduce((acc, val) => acc.concat(val), [])
arr.flatMap(x => x)
arr.flat()
arr.reduce((acc, val) => {
if (val) val.forEach(a => acc.push(a));
return acc;
}, [])
let acc = [];
arr.forEach(val => {
val && val.forEach(v => acc.push(v));
}), acc;
[].concat(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce | |
flatMap | |
flat | |
reduce push | |
forEach push | |
concat spread |
Test name | Executions per second |
---|---|
reduce | 22759.2 Ops/sec |
flatMap | 44858.0 Ops/sec |
flat | 45440.5 Ops/sec |
reduce push | 45093.3 Ops/sec |
forEach push | 43681.6 Ops/sec |
concat spread | 521171.6 Ops/sec |
Measuring the performance of different JavaScript array methods can be a complex task, as it depends on various factors such as the input data, hardware, and software configurations.
The benchmark provided by MeasureThat.net compares six array methods: flat
, flatMap
, reduce
, reducePush
, forEachPush
, and concatSpread
. Here's what each method does:
flat()
: Returns a new array with all sub-array elements concatenated into it.flatMap()
: Similar to map()
, but returns an array of the mapped values rather than an array of arrays.reduce()
: Applies a function against an accumulator and each element in the array, going from left to right, so as to reduce it to a single output value. The function takes four arguments: the accumulator, the current element, the index, and the array.reducePush()
: Similar to reduce()
, but instead of returning a single output value, it pushes each element onto an accumulator array.forEachPush()
: Iterates over the array and calls a callback function for each element, pushing the result onto an accumulator array.concatSpread()
: Concatenates all elements in the array into a new array using the spread operator (...
).Now, let's discuss the pros and cons of each approach:
flat()
:flatMap()
:map()
because it avoids creating an array of arrays, which can be expensive in terms of memory. It also provides a way to flatten arrays.Array.prototype.map()
).reduce()
:reducePush()
:reduce()
, but with an added advantage of pushing elements onto an accumulator array instead of returning a single output value. This can be beneficial when working with large datasets or when memory allocation is not a concern.forEachPush()
:reducePush()
, as it doesn't create an intermediate accumulator array.As for the libraries used in these methods:
Array.prototype.flatMap()
: Introduced in ECMAScript 2019 (ES10), flatMap()
provides a concise way to flatten arrays.Array.prototype.flat()
: Introduced in ECMAScript 2017 (ES7), flat()
allows you to specify the maximum depth of array flattening.Regarding special JavaScript features or syntax:
flatMap()
utilizes the map()
method and its return value.=>
), async/await, or Promises.Now that we have covered the key points, here are some alternatives:
flatten()
, flattenDeep()
, and mapValues()
.In conclusion, understanding the differences between these JavaScript array methods can be beneficial when optimizing performance or choosing the most suitable approach for a specific task.