Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
6 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] = 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; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
1145.9 Ops/sec
Native
2520.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark, specifically comparing the performance of two approaches for grouping an array: `_.groupBy` from the Lodash library and a native implementation using the `reduce` method. **What is being tested?** The benchmark is testing the performance difference between these two approaches on a dataset of 100,000 objects with unique IDs. The test cases are designed to measure which approach is faster in terms of executions per second. **Options compared:** 1. **Lodash groupBy**: This method uses the Lodash library to perform grouping. It takes an array and a function that extracts the key from each element. In this case, the function is `({ id }) => id`. 2. **Native reduce**: This approach uses the built-in `reduce` method of JavaScript arrays to achieve similar results. The callback function accumulates objects with the same ID in the accumulator. **Pros and Cons of each approach:** 1. **Lodash groupBy**: * Pros: + Easier to write and maintain, especially for complex grouping logic. + Often more efficient than native `reduce` due to optimizations provided by Lodash. * Cons: + Adds dependency on the Lodash library, which may not be desirable in all scenarios. 2. **Native reduce**: * Pros: + Lightweight and doesn't introduce any additional dependencies. + Provides a deep understanding of how JavaScript arrays work internally. * Cons: + Can be more verbose and harder to read for complex grouping logic. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, object normalization, and functional programming. The `groupBy` function is one of its many useful utilities. It takes an array and a predicate function as arguments, groups the elements based on the output of the predicate, and returns an object with each group as a key. **Special JavaScript feature: ES6 arrow functions** The `_.groupBy` function uses an arrow function to extract the ID from each element in the dataset. Arrow functions are a concise way to define small, single-purpose functions without creating a new scope. They were introduced in ECMAScript 2015 (ES6) and provide several benefits, including reduced syntax clutter and improved readability. **Benchmark results** The latest benchmark result shows that the native `reduce` approach is slightly faster than the Lodash `groupBy` approach on this particular test case. However, it's essential to note that performance differences might vary depending on the specific use case and dataset. **Other alternatives** If you're looking for alternative approaches or libraries for grouping arrays, consider the following options: 1. **Array.prototype.reduce()**: This is a standard JavaScript method that achieves similar results as the native `reduce` approach. 2. **Underscore.js**: Another popular utility library that provides a `groupBy` function similar to Lodash's implementation. 3. **Ramda**: A functional programming library that offers a `groupBy` function with various options for customizing the grouping behavior. Ultimately, the choice of approach depends on your project's requirements, personal preference, and performance considerations.
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?