Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k - test
(version: 0)
Comparing performance of:
Lodash vs Native vs Prebuilt keys
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 = []; var keys = new Array(max2).map((item, index) => index); 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; }, {})
Prebuilt keys
keys.map((key) => { return data.filter((item) => item.id); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
Native
Prebuilt keys
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing three approaches to group data by an id field: 1. **Lodash**: Using the `_groupBy` function from the Lodash library to group the data. 2. **Native**: Using the `reduce` method on the `data` array to achieve the same result. 3. **Prebuilt keys**: Creating a separate array of keys (`keys`) and using it to filter the data. **Options Compared** * `_.groupBy(data, ({ id }) => id)`: This option uses the Lodash library's `groupBy` function, which takes an array and a callback function that extracts the group key from each element. * `data.reduce((acc, item) => { ... }, {})`: This option uses the `reduce` method on the `data` array, which applies a reduction function to each element in the array. In this case, the accumulator (`acc`) is an object that's being built up as we iterate through the array. * `keys.map((key) => { return data.filter((item) => item.id); })`: This option uses an array of keys and maps over it, filtering the `data` array for each key in the process. **Pros and Cons** * **Lodash**: Pros: + Concise and readable code + Well-tested and maintained library + Easy to use * Cons: + Adds an external dependency (the Lodash library) + May not be suitable for very large datasets * **Native**: Pros: + No external dependencies required + Can be faster due to reduced overhead of function calls + Can be more memory-efficient * Cons: + More verbose code compared to the Lodash version + May require more development effort to implement correctly + May have performance issues if not implemented carefully * **Prebuilt keys**: Pros: + No external dependencies required + Can be faster due to reduced overhead of function calls + Can be more memory-efficient * Cons: + More complex code compared to the Lodash version + Requires manual management of the key array **Library and Purpose** The `_.groupBy` function from Lodash is used to group an array by a specific property. In this case, it's used to group the data array by the `id` field. **Special JS Feature or Syntax** There are no special JavaScript features or syntax mentioned in the provided code snippets. The use of functions, arrays, and objects is standard JavaScript syntax. **Other Alternatives** If you want to avoid using external libraries like Lodash, you can also use other grouping algorithms such as: * Using a library like `console.group` (a part of the built-in console object) to group data * Implementing a custom grouping algorithm using only built-in JavaScript methods and functions * Using a third-party library specifically designed for grouping data, such as `moment.js` Keep in mind that each alternative has its pros and cons, and may not offer the same level of readability or performance as the original Lodash implementation.
Related benchmarks:
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k corrected
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?