Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce on million items
(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) => { 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
37.3 Ops/sec
Native
104.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared options, pros and cons of each approach, library usage, special JS features or syntax (if any), and alternatives. **Benchmark Overview** The benchmark compares two approaches for grouping an array of objects by a common property: Lodash's `groupBy` function and JavaScript's built-in `reduce` method. The test case uses one million items in the data array. **Script Preparation Code** The script preparation code creates an array `data` with 1,000,000 objects, each having an `id` property. ```javascript var max2 = 1000000; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); } ``` **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library, which will be used by the `groupBy` function. ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` **Benchmark Test Cases** There are two test cases: 1. **Lodash**: The first test case uses Lodash's `groupBy` function to group the data array by the `id` property. ```javascript _.groupBy(data, ({ id }) => id); ``` 2. **Native (reduce)**: The second test case uses JavaScript's built-in `reduce` method to achieve the same grouping result. **Pros and Cons of Each Approach** 1. **Lodash (`groupBy`)**: * Pros: + Well-tested and optimized function. + Easy to use and understand. * Cons: + Adds an extra dependency (the Lodash library). + May have slower performance compared to the native `reduce` method. 2. **Native (`reduce`)**: * Pros: + No additional dependencies or overhead. + Can be optimized for performance by using a hash table-like data structure. * Cons: + Requires more code and complexity to implement correctly. + May have slower performance compared to the Lodash `groupBy` function. **Library Usage** The test case uses the Lodash library's `groupBy` function, which is a popular utility function for grouping arrays by a common property. The library provides a convenient and efficient way to achieve this result without writing custom implementation. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Alternatives** If you prefer not to use the Lodash library, you can implement the `groupBy` functionality using only built-in JavaScript methods and data structures. One possible approach is to use an object with nested objects as keys: ```javascript const groupedData = {}; for (let i = 0; i < data.length; i++) { const key = data[i].id; if (!groupedData[key]) { groupedData[key] = []; } groupedData[key].push(data[i]); } ``` This approach has the same functionality as Lodash's `groupBy` function but requires more code and may have slower performance. Another alternative is to use a library like Ramda, which provides a similar `groupBy` function that can be used instead of Lodash. ```javascript import { groupBy } from 'ramda'; const groupedData = groupBy((item) => item.id)(data); ``` These alternatives require more code and setup but provide an alternative implementation for the `groupBy` functionality.
Related benchmarks:
lodash groupBy vs Array.reduce (1mln)
lodash groupBy vs Array.reduce (2)
lodas2 groupBy vs Array.reduce
lodash groupBy vs Array.reduce 100k with array push
Comments
Confirm delete:
Do you really want to delete benchmark?