Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce (group count)
(version: 1)
Comparing performance of:
reduce with concat vs flatMap
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10).fill({items: Array(1000).fill(0)})
Tests:
reduce with concat
arr.reduce((acc, x) => acc + x.length)
flatMap
arr.flatMap(x => x.items).length
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce with concat
9139557.0 Ops/sec
flatMap
9301.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided compares the performance of two JavaScript approaches for grouping and counting items within an array of objects: using `reduce` versus using `flatMap`. ### Benchmark Overview - **Name:** flatMap vs reduce (group count) - **Input Data:** An array of 10 objects, each containing a `items` key that maps to an array of 1000 zeros. This setup is meant to simulate a scenario where we're interested in counting the total number of items across all objects. ### Test Cases Detailed 1. **Test Case: reduce with concat** - **Benchmark Definition:** `arr.reduce((acc, x) => acc + x.length)` - **Purpose:** This test case uses the `reduce` method to accumulate the lengths of the objects within the array. It iteratively adds the `length` of each object in `arr` to a running total (`acc`). - **Pros:** - It is a functional programming approach that can be quite readable for those familiar with JavaScript's array methods. - Works with older versions of JavaScript, and compatibility is generally good. - **Cons:** - As the implementation relies on accumulating the length property, it may inherently create a new immutable array in the process, which could be less efficient than a direct approach. - The performance may degrade with larger datasets due to repeated addition operations. 2. **Test Case: flatMap** - **Benchmark Definition:** `arr.flatMap(x => x.items).length` - **Purpose:** This test case uses the `flatMap` method to first flatten the array of `items` and then retrieves the total count by checking the length of the flattened array. - **Pros:** - `flatMap` combines mapping and flattening in a single method, making it a one-step process to achieve the desired structure. - It can provide better performance for large nested structures as it avoids the need to chunk results in multiple steps. - **Cons:** - It is relatively newer and might not be compatible with older JavaScript engines, specifically prior to ES2019. - Using `flatMap` can lead to increased memory usage depending on the size of the arrays being processed because it creates an intermediary flattened array. ### Benchmark Results The benchmark results show a clear performance difference: - **reduce with concat:** Achieved **8,358,828.5 executions per second**. - **flatMap:** Achieved **10,361.7 executions per second**. ### Conclusion and Alternatives In this benchmark, using `reduce` significantly outperformed `flatMap`. This suggests that while `flatMap` is a powerful and potentially easier-to-read method for flattening and mapping arrays, it may have some overhead that impacts performance in specific cases, particularly with simpler operations like counting lengths. ### Other Alternatives Alternative approaches to achieve similar tasks could include: - **Using a simple loop:** Iterating through the array to accumulate counts, which may even outperform both methods in some scenarios. - **Using `forEach` in conjunction with a manual accumulator**: Similar to `reduce`, but may allow for potentially clearer syntax at the cost of not being purely functional. - **Using libraries:** Libraries like Lodash offer utility functions (`_.sumBy`) that can simplify and potentially optimize array operations but come with their own performance overhead. Each of these alternatives varies in terms of readability, performance, and compatibility; thus, the choice can depend on specific use cases and coding standards in your application.
Related benchmarks:
flatMap vs reduce
flatMap vs reduce (with concat())
flatMap vs reduces
flatMap vs reduce (concat)
flatMap vs reduce small array
flat map vs reduce concat
flatMap vs reduce (concatenation)
flatMap vs reduce 2
flatMap vs reduce flattern array
flatMap vs reduce (group count) (v2)
Comments
Confirm delete:
Do you really want to delete benchmark?