Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k v2
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
5 years 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 = 1000000; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
Lodash
_.groupBy(data, ({ id }) => id)
Native
data.reduce((acc, item) => { if (acc[item.id]) { acc[item.id].push(item); } else { acc[item.id] = [item]; } 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is comparing two approaches to group data by an ID: using Lodash's `groupBy` function versus using JavaScript's built-in `Array.prototype.reduce` method. **Options Compared** Two options are being compared: 1. **Lodash's `groupBy`**: This function groups the input array into arrays of equal keys, where the keys are unique and the values are arrays. 2. **Native `Array.prototype.reduce`**: This is a built-in JavaScript method that applies a reduction operation to an array. **Pros and Cons** * **Lodash's `groupBy`** + Pros: - Easier to read and write, as it uses a more functional programming style. - More concise code. + Cons: - Requires the Lodash library, which may introduce additional overhead. - May have slower performance due to the use of an external library. * **Native `Array.prototype.reduce`** + Pros: - Built-in functionality, so no external libraries are required. - Often faster and more efficient than using a library. + Cons: - More verbose code, making it harder to read and write. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks. In this case, `groupBy` is used to group the input array by an ID. **Special JS Feature/Syntax (Optional)** There are no special JavaScript features or syntaxes being tested in this benchmark. The code uses standard JavaScript syntax and built-in methods. **Other Alternatives** If you want to implement a custom grouping algorithm, you could use other approaches like: * Using `Array.prototype.forEach` and creating an object with dynamic properties. * Using `Array.prototype.reduce` with a custom accumulator function that updates the result object. * Using a library like Moment.js or Date-time formatting libraries if you need more complex date-based grouping. However, these alternatives are not being tested in this specific benchmark. **Benchmark Preparation Code** The provided preparation code creates an array of objects with `id` properties, where each ID ranges from 0 to 1 million. This large dataset is used to simulate a real-world scenario and make the benchmark more relevant. **Individual Test Cases** There are two test cases: 1. **Lodash** 2. **Native (Array.prototype.reduce)** Each test case uses one of these approaches to group the input data by ID, using either Lodash's `groupBy` function or JavaScript's built-in `Array.prototype.reduce` method, respectively. **Latest Benchmark Result** The benchmark results show that: * Chrome 80 on a Linux desktop performs better with the Native approach (executions per second: 5.408844947814941). * Chrome 80 on a Linux desktop performs better with Lodash's `groupBy` approach (executions per second: 6.187389373779297). This suggests that, in this specific scenario, using the built-in `Array.prototype.reduce` method might be slightly faster than using Lodash's `groupBy` function. However, it's essential to note that these results may vary depending on the actual use case and performance requirements.
Related benchmarks:
lodash groupBy vs Array.reduce (2)
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
Comments
Confirm delete:
Do you really want to delete benchmark?