Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs group with Array.reduce 100k
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var max2 = 100000; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
Lodash
_.groupBy(data, ({ id }) => id)
Native
data.reduce((acc, item) => { curr = acc[item.id] || [] curr.push(item) acc[item.id] = curr; return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Native
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
160.6 Ops/sec
Native
23.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of two approaches to group data by a specific field: `_.groupBy` from the Lodash library and `Array.reduce`. Both approaches are used to group an array of objects with an `id` property. **Lodash Approach (`_.groupBy`)** The Lodash approach uses the `_.groupBy` function, which groups the data by a specified function. In this case, the function is `{ id } => id`, which simply returns the value of the `id` property for each object in the array. The pros of using `_.groupBy` are: * Concise and readable code * No need to manually manage the group objects * Lodash provides a robust implementation with error handling and other features However, the cons are: * Additional dependency on the Lodash library * May have overhead due to the function call and object creation * Less control over the grouping process compared to the native approach **Native Approach (`Array.reduce`)** The native approach uses `Array.reduce` to group the data. The reducer function iterates through the array, and for each item, it checks if there is an existing group object in the accumulator (`acc`). If not, it creates a new group object with an empty array. It then pushes the current item into the corresponding group array and updates the group object in the accumulator. The pros of using `Array.reduce` are: * No additional dependency on libraries * More control over the grouping process (e.g., custom key function) * Can be optimized for specific use cases However, the cons are: * More verbose code compared to the Lodash approach * Requires manual management of group objects and array updates **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, object manipulation, and more. In this benchmark, `_.groupBy` is used to perform the grouping operation. **Special JavaScript Feature or Syntax** The test case uses the `=>` operator, which is an arrow function syntax introduced in ECMAScript 2015 (ES6). Arrow functions provide a concise way to define small, single-expression functions. In this benchmark, the `=>` operator is used to define the grouping function for Lodash. **Other Alternatives** Alternative approaches to group data include: * Using a custom object with nested properties: `{ groups: {} }`. This approach requires manual management of the group objects and array updates. * Using a library like Underscore.js or Ramda, which provide similar grouping functions to Lodash. * Implementing a custom grouping algorithm using vanilla JavaScript. Note that these alternatives may have different trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
lodas2 groupBy vs Array.reduce
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k with array push
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs Object.groupBy 100k
Comments
Confirm delete:
Do you really want to delete benchmark?