Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce performance
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
3 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; // 100,000,000 (100 Million) var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
Lodash
_.groupBy(data, '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:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
39.8 Ops/sec
Native
116.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test. **Benchmark Overview** The test compares the performance of two approaches to group an array by a specific key: using the `lodash.groupBy` function and using the built-in `Array.reduce` method. **Test Cases** There are two test cases: 1. **Lodash**: This test case uses the `lodash.groupBy` function to group the data array by the "id" key. 2. **Native**: This test case uses the built-in `Array.reduce` method to achieve the same grouping result. **Options Compared** The options being compared are: * Using an external library (`lodash`) versus relying on built-in JavaScript functionality (`Array.reduce`). **Pros and Cons of Each Approach** ### Lodash Pros: * Easier to implement, especially for those unfamiliar with `Array.reduce`. * Provides a simple and concise API for grouping data. * Often used in production codebases, so developers may be more comfortable with its syntax. Cons: * Adds an external dependency (the `lodash` library), which can increase the size of the bundle and potentially slow down page loads. * May not be suitable for small projects or those targeting very basic browsers. ### Native (Array.reduce) Pros: * No external dependencies, making it a good choice for small projects or projects targeting very basic browsers. * Can be more performant since it doesn't require an additional library to execute. Cons: * Requires knowledge of `Array.reduce` syntax and implementation details, which can be challenging for some developers. * May not be as readable or maintainable as the Lodash implementation, especially for complex grouping scenarios. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for data manipulation, string manipulation, and more. The `groupBy` function is one of its most useful tools, allowing developers to easily group arrays by specific keys. In this test case, Lodash serves as the external dependency being compared against the native `Array.reduce` implementation. **Special JS Feature/Syntax: None** Neither of the tested approaches relies on any special JavaScript features or syntax beyond standard ES6 and ES7 features (such as arrow functions and template literals). **Other Alternatives** If you wanted to add more test cases, you could consider: * Using other external libraries like `underscore` or `ramda`. * Implementing grouping using `Array.prototype.forEach`, `Array.prototype.forEachRight`, or other custom looping mechanisms. * Comparing performance with different data structures (e.g., objects instead of arrays). * Investigating how the test results change under various optimization settings or hardware configurations. Feel free to ask if you have any further questions!
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?