Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.groupBy vs manual groupBy
(version: 0)
Comparing performance of:
Object.groupBy vs Manual groupBy
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function groupBy(array, mapper) { return array.reduce( (acc, item) => { const key = mapper(item); const items = acc[key] ?? (acc[key] = []); items.push(item); return acc; }, {}, ); } var data = [{ name: "John", age: 20, occupation: null, }, { name: "Jane", age: 30, occupation: "student", }, { name: "Jack", age: 40, occupation: "student", }, { name: "Simon", age: 50, occupation: "student", }, { name: "Mary", age: 60, occupation: null, }, { name: "George", age: 70, occupation: "influencer", }, ]
Tests:
Object.groupBy
return Object.groupBy(data, item => item.occupation)
Manual groupBy
return groupBy(data, item => item.occupation)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.groupBy
Manual groupBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 138 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.groupBy
3796548.2 Ops/sec
Manual groupBy
8999024.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **What is being tested?** The benchmark compares two approaches to group data by occupation using either the `Object.groupBy()` method or a custom implementation called `groupBy()`. The test case is designed to measure the performance of each approach on a sample dataset containing 6 objects with varying ages and occupations. **Options compared:** 1. **`Object.groupBy()`**: This method uses the built-in `groupBy()` function from the JavaScript Object prototype. 2. **Custom implementation (`groupBy()`)**: A custom function that takes an array and a mapper function as arguments, which is used to group the data by occupation. **Pros and Cons of each approach:** 1. **`Object.groupBy()`**: * Pros: + Built-in method, so it's well-supported across browsers. + Simplifies code, as it doesn't require manual looping or recursion. * Cons: + Might be slower due to the overhead of calling a built-in function. + May not be suitable for all use cases, as its behavior is specific to the Object prototype. 2. **Custom implementation (`groupBy()`)**: * Pros: + Allows for more control over the grouping logic and performance optimization. + Can be optimized for specific use cases or data structures. * Cons: + Requires manual looping or recursion, which can increase code complexity. + May not be supported across all browsers, especially older ones. **Library:** The `groupBy()` function used in the custom implementation is a built-in JavaScript method that groups an array of objects by a specified key (in this case, occupation). It's a simple and efficient way to perform data aggregation. **Special JS feature or syntax:** None mentioned in this benchmark. However, it's worth noting that `Object.groupBy()` relies on the `map` function, which is a standard JavaScript method for creating new arrays by applying a transformation function to each element of an existing array. **Alternative approaches:** 1. **Using `reduce()`**: Instead of using `groupBy()`, you could use the `reduce()` function to achieve similar results. 2. **Using a library like Lodash**: The `groupBy()` function is also available in the popular Lodash library, which provides a more comprehensive set of utility functions for JavaScript development. In summary, the benchmark compares two approaches to group data by occupation: using the built-in `Object.groupBy()` method versus a custom implementation. The choice between these approaches depends on the specific use case and performance requirements.
Related benchmarks:
array to obj by key value
array to obj by key value advanced
Reduce with spread VS for...of with push
Reduce vs Object.groupBy
Comments
Confirm delete:
Do you really want to delete benchmark?