Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce using push spread
(version: 0)
Comparing performance of:
reduce with concat vs flatMap
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10_000).fill({arr: [0,1]})
Tests:
reduce with concat
arr.reduce((acc, x) => {acc.push(...x.arr); return acc}, [])
flatMap
arr.flatMap(x => x.arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce with concat
flatMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The test compares two approaches: using `reduce()` with `concat` (or equivalently, `.push()`) versus using `flatMap()` on an array of arrays. **Script Preparation Code** ```javascript var arr = Array(10_000).fill({arr: [0,1]}); ``` This code creates a large array (`arr`) containing 10,000 objects, each with an `arr` property that contains two elements: `[0, 1]`. **Benchmark Definitions** There are two benchmark definitions: 1. **"reduce with concat"** ```javascript arr.reduce((acc, x) => { acc.push(...x.arr); return acc }, []); ``` This code uses the `reduce()` method to iterate over the array and push each element of the nested arrays into a new array (`acc`). The resulting array is then returned. 2. **"flatMap"** ```javascript arr.flatMap(x => x.arr); ``` This code uses the `flatMap()` method, which is a more modern replacement for `.concat()` with `.map()`. It returns a new array with the elements of each nested array, without creating intermediate arrays. **Library and Special Features** In this benchmark, we're using JavaScript's built-in Array methods: * `.reduce()` * `.flatMap()` * `.push()` No external libraries are used in this test. **Approach Comparison** The two approaches have different performance characteristics: 1. **"reduce with concat"**: This approach uses a simple iterative method to build the result array, which can be slower due to the overhead of function calls and array concatenation. 2. **"flatMap"**: This approach is generally faster because it avoids the need for explicit iteration and array concatenation. However, modern browsers have optimized `flatMap()` to perform similarly to `.concat()`, so the performance difference may be smaller than expected. **Pros and Cons** * **"reduce with concat"**: + Pros: straightforward implementation, easy to understand. + Cons: slower due to array concatenation. * **"flatMap"**: + Pros: faster, more modern method, less prone to errors. + Cons: may require more time to understand, depends on browser optimizations. **Other Considerations** When choosing between these approaches, consider the following: * If you need to process a large array with nested elements and don't care about performance, `reduce()` might be a better choice due to its simplicity. * If performance is critical and you're using modern browsers that support `flatMap()`, this method may be a better option. **Alternatives** Other methods for flattening arrays include: 1. **`.map()`: arr.map(x => x.arr).flat()** 2. **`.forEach()`**: arr.forEach(x => { const result = [...x.arr]; // do something with result }); 3. **Looping manually using `for` and array indexing**
Related benchmarks:
flatMap vs reduce using push
Reduce Push vs. flatMap with subarrays
flatMap vs reduce spread vs reduce push
flatMap vs Reduce with push - test
Comments
Confirm delete:
Do you really want to delete benchmark?