Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k with gey generation 2
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
2 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; const toKey = (item) => item.date.toLocaleDateString() function zeroOutTime(date) { const newDate = new Date(date.getTime()); newDate.setHours(0, 0, 0, 0); return newDate; } var data = []; for (var i = 0; i <= max2; i++) { const key = zeroOutTime(new Date(Date.now() + Math.floor(Math.random()*1000*60*60*24*30))).valueOf() data.push({ id: i, key: key }); }
Tests:
Lodash
const toKey = (item) => item.key; _.groupBy(data, toKey)
Native
const toKey = (item) => item.key; data.reduce((acc, item) => { acc[toKey(item)] = 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 explain what is being tested. **Benchmark Definition** The benchmark measures the performance of two approaches for grouping data: Lodash's `groupBy` function and the native JavaScript `Array.reduce` method. **Options Compared** * **Lodash's groupBy**: This approach uses the `groupBy` function from the Lodash library, which takes an array as input and a key generator function. The key generator function is used to extract a unique value from each element in the array, and then groups the elements by these values. * **Native Array.reduce**: This approach uses the `Array.reduce` method without any additional libraries or modifications. It also groups the elements of an array by a specified key. **Pros and Cons** * **Lodash's groupBy**: + Pros: High-level abstraction, easy to read and understand, well-tested. + Cons: Adds an extra dependency on Lodash library, may have slower performance due to additional overhead. * **Native Array.reduce**: + Pros: No additional dependencies, potentially faster performance since it doesn't add any extra overhead. + Cons: Requires manual key generation, which can be error-prone and harder to understand for beginners. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. The `groupBy` function is one of its most useful functions, allowing you to group elements in an array by a specified key. In this benchmark, Lodash's `groupBy` function is used as the alternative to the native `Array.reduce` method. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being used in these benchmarks. They only rely on standard JavaScript functionality and libraries (Lodash). **Other Alternatives** If you don't want to use Lodash, there are other ways to group elements by a key: * Using `Array.prototype.reduce` with a custom callback function. * Using `Array.prototype.forEach` and creating an object to store the grouped elements. However, these alternatives may not be as efficient or convenient as using Lodash's `groupBy` function. **Benchmark Preparation Code** The preparation code is responsible for setting up the data used in the benchmark. In this case: * It creates an array of 100,000 objects with a unique key generated by a custom `zeroOutTime` function. * The key generation function uses the `Date` object and generates a timestamp that will be converted to a string using `toLocaleDateString`. The preparation code is important because it sets up the data that will be used in the benchmark. It ensures that the same data is being used for both benchmarks (Lodash's groupBy and native Array.reduce) and allows us to compare their performance. **Benchmark Results** The latest benchmark results show: * **Native Array.reduce**: 78.568 executions per second. * **Lodash's groupBy**: 55.691 executions per second. These results suggest that Lodash's `groupBy` function is slightly slower than the native `Array.reduce` method, but both are relatively fast and efficient.
Related benchmarks:
lodash groupBy vs Array.reduce (1mln)
lodash groupBy vs Array.reduce (2)
lodash groupBy vs Array.reduce 100k - test
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs simple for loop
Comments
Confirm delete:
Do you really want to delete benchmark?