Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce (test1)
(version: 1)
Comparing performance of:
reduce vs flatMap
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10_000).fill(0)
Tests:
reduce
arr.reduce((acc, x) => { acc.push(...[x]); return acc; }, [])
flatMap
arr.flatMap(x => [x])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
flatMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
27705.2 Ops/sec
flatMap
5641.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark is designed to compare the performance of two different JavaScript approaches for creating a new array based on the contents of an existing array. Specifically, it evaluates the performance of using the `reduce` method versus the `flatMap` method. ### 1. **Tested Approaches** - **reduce**: ```javascript arr.reduce((acc, x) => { acc.push(...[x]); return acc; }, []) ``` This approach uses the `reduce` method to accumulate results. It takes each element `x` in the array `arr` and pushes it into an accumulator array `acc`, effectively building a new array from the values in `arr`. The spread operator (`...`) is used to expand the single value `[x]` into the accumulator. - **flatMap**: ```javascript arr.flatMap(x => [x]) ``` The `flatMap` method is a combination of `map` followed by `flat`. Here, it maps each element of the array to a new array containing the single element `[x]`, and then flattens the result by one level. Since we're effectively mapping to single-element arrays, it behaves similarly to the `reduce` method in this scenario. ### 2. **Performance Results** From the benchmark results, we can see the following: - **reduce** had a performance of **27,705.21 executions per second**. - **flatMap** had a performance of **5,641.62 executions per second**. The `reduce` method outperformed `flatMap` significantly in this test case. ### 3. **Pros and Cons** **reduce**: - **Pros**: - Offers flexible accumulation patterns thanks to its custom callback. - Can be used for diverse operations beyond mere transformation (e.g., summing values). - **Cons**: - Slightly more verbose and complex syntax compared to `flatMap`. - Performance can degrade with very large datasets due to the accumulation pattern. **flatMap**: - **Pros**: - Concise and readable syntax for certain common transformations. - Automatically handles flattening the result, avoiding nested arrays. - **Cons**: - Slower in this specific case compared to `reduce`. - Less flexible than `reduce`, limited to transformations that result in one level of flattening. ### 4. **Other Considerations** While the benchmark focuses on two specific methods, it highlights the broader consideration of choosing the right tool for the job based on both performance and readability. Other alternatives for creating a new array from an existing array include: - Using `map` followed by `flat`, though this would add an additional iteration step and is typically not as performant as using `flatMap`. - A simple `for` loop, which can sometimes be optimized by the JavaScript engine more efficiently than higher-order functions, although it lacks the concise syntax of functional methods. Ultimately, the choice between `reduce`, `flatMap`, or any other method should consider readability, performance, and the specific requirements of the code being written. In cases with very large datasets or critical performance needs, testing multiple approaches, as shown here, can help identify the best solution.
Related benchmarks:
flatMap vs reduce
Reduce Push vs. flatMap
flatMap vs reduce using push
flatMap vs reduce (with concat())
flatMap vs reduces
reduce vs. flatMap v3
Reduce vs flatMap performance
flat map vs reduce concat
flatMap vs reduce Saran
flatMap vs Reduce with push - test
Comments
Confirm delete:
Do you really want to delete benchmark?