Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Object.groupBy with function 2
(version: 1)
Testing the new native method
Comparing performance of:
New native groupBy vs Old native reduce
Created:
11 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var max2 = 100000; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); } const getId = ({ id }) => id;
Tests:
New native groupBy
Object.groupBy(data, getId)
Old native reduce
data.reduce((acc, item) => { const id = getId(item); if (acc[id]) { acc[id].push(item) } else { acc[id] = [item] } return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
New native groupBy
Old native reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
New native groupBy
112.8 Ops/sec
Old native reduce
534.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
The benchmark defined in the JSON is designed to compare two different approaches for grouping objects in an array by a specific property—in this case, the `id` property of each object. The focus of the benchmarks is on evaluating the performance of the new `Object.groupBy` method against the more traditional `Array.prototype.reduce` method. ### Approaches Being Compared 1. **New Native `Object.groupBy` Method**: - **Benchmark Definition**: `Object.groupBy(data, getId)` - **Test Name**: "New native groupBy" - **Purpose**: The `Object.groupBy` method is a proposed native JavaScript method (though, as of the last update, it might still be stage 3 in TC39 proposals). If available in the JavaScript runtime, this method simplifies grouping objects by a key into an object with arrays of grouped items, providing a more readable and potentially more optimized native treatment of such tasks. - **Pros**: - Enhanced readability and maintainability of the code. - Optimized internally by JavaScript engines. - Reduction of boilerplate code. - **Cons**: - Limited support in older browsers and JavaScript environments. - Might not be available in all JavaScript runtime versions. 2. **Old Native `Array.prototype.reduce` Method**: - **Benchmark Definition**: `data.reduce((acc, item) => { const id = getId(item); if (acc[id]) { acc[id].push(item) } else { acc[id] = [item] } return acc; }, {})` - **Test Name**: "Old native reduce" - **Purpose**: The `reduce` method iterates over the array and accumulates results into a single object. It requires understanding of how accumulation works and sometimes results in more verbose code. - **Pros**: - Fine-grained control over how accumulation is handled. - Very widely supported across all JavaScript environments. - **Cons**: - More verbose and less intuitive than dedicated grouping methods. - Potentially less efficient as it manually manages the accumulation logic. ### Results Interpretation From the benchmark results provided: - The performance of the old `reduce` method was significantly better (534.90 executions per second) compared to the new `groupBy` method (112.75 executions per second) on the tested environment (Chrome 136 on Mac OS X 10.15.7). - This discrepancy likely highlights the need for further optimization in the implementation of `Object.groupBy`, or it could also reflect the current state of the JavaScript engine's optimization for this method. ### Other Considerations and Alternatives - **Performance Variability**: The performance of JavaScript methods can vary significantly across different environments (browsers, versions, devices). While `Object.groupBy` may eventually become more performant as browsers evolve, it's essential to consider current limitations. - **Utility Libraries**: There are various utility libraries like Lodash that provide similar grouping capabilities (`_.groupBy`). These libraries could be a good alternative where performance considerations are critical and browser compatibility is a concern, although they will add to the bundle size. Overall, while native methods promise better performance and cleaner code, developers should weigh their decisions based on the context of their projects, particularly regarding performance implications in the browsers they support.
Related benchmarks:
lodash groupBy vs Array.reduce(3 ways) 10k
Group By Comparison; For, For Of, and Reduce
lodash groupBy vs Array.reduce vs Object.fromEntries 100k
lodash groupBy vs Array.reduce vs Object.groupBy 100k
lodash groupBy vs Object.groupBy 100k
Reduce vs Object.groupBy
lodash groupBy vs group with Array.reduce 100k
Reduce vs Array.groupBy
Reduce vs Object.groupBy with function
Comments
Confirm delete:
Do you really want to delete benchmark?