Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce [correct] 100k
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
4 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 = 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) => { acc[item.id] = (acc[item.id] || []).concat(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
289.5 Ops/sec
Native
89.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 the pros/cons of each approach. **Benchmark Overview** The test is comparing two approaches to group an array of objects by their "id" property: 1. **Lodash**: Using the `_groupBy` function from the Lodash library. 2. **Native**: Implementing a custom grouping logic using the `reduce` method. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks, such as data manipulation, string manipulation, and more. The `_groupBy` function in particular is designed to group an array by a specified key (in this case, the "id" property). **Pros and Cons of Lodash Approach** Pros: * Shorter code: The implementation using Lodash is concise and easy to read. * Efficient: Lodash provides optimized implementations for many algorithms, including grouping. Cons: * External dependency: Your application needs to include the Lodash library, which may add to its bundle size or complexity. * Overhead: Adding an external library can introduce additional overhead due to loading times or performance impact. **Pros and Cons of Native Approach** Pros: * No external dependencies: You don't need to include any libraries, making it a more lightweight option. * Flexibility: You have full control over the implementation details. Cons: * Longer code: The custom `reduce` implementation can be more verbose than using Lodash. * Potential performance impact: Without optimization, the native approach might not be as efficient as the optimized Lodash implementation. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax being used in this benchmark. Both approaches rely on standard JavaScript features (arrays, functions, loops) and do not utilize any experimental or proposed features. **Other Alternatives** If you're looking for alternative grouping libraries or algorithms, some options include: * **Array.prototype.reduce()**: While not as concise as Lodash's implementation, using `reduce()` is a native JavaScript method that can achieve similar results. * **ES6 Array.prototype.groupBy()**: Introduced in ECMAScript 2019 (ES11), this is a built-in method for grouping arrays by a specified key. However, it might not be supported by older browsers or environments. * **Other libraries like Ramda or Lisk**: These libraries offer alternative implementations of group-by functions, often with additional features or optimizations. In summary, the benchmark provides a fair comparison between using an external library (Lodash) versus implementing a custom grouping logic using native JavaScript. The choice between these approaches depends on your specific needs, performance requirements, and personal preference regarding code complexity and dependencies.
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?