Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce vs Object.fromEntries 100k
(version: 0)
Comparing performance of:
Lodash groupBy vs Array.reduce vs Object.fromEntries
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Script Preparation code:
var max2 = 100000; var data = Array(max2).fill(0).map((_, i) => ({id: i}));
Tests:
Lodash groupBy
_.groupBy(data, ({ id }) => id)
Array.reduce
data.reduce((acc, item) => { acc[item.id] = item; return acc; }, {})
Object.fromEntries
Object.fromEntries(data.map(item => [item.id, item]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash groupBy
Array.reduce
Object.fromEntries
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):
Measuring the performance of different approaches to grouping data in JavaScript. The benchmark defines three test cases: 1. **Lodash groupBy**: This approach uses the `_groupBy` function from Lodash, a popular utility library for JavaScript. The `_.groupBy` function takes an array of objects and a callback function that extracts the key from each object. It returns an object with keys being the extracted values and values being arrays of corresponding objects. 2. **Array.reduce**: This approach uses the `reduce` method on the `data` array. The `reduce` method applies a callback function to each element in the array, accumulating a result. In this case, it groups the data by iterating over each object and adding its id as a key in the accumulator, which is initialized as an empty object. 3. **Object.fromEntries**: This approach uses the `Object.fromEntries` method, which creates a new object from an iterable of key-value pairs. **Options comparison** The three approaches have different pros and cons: * **Lodash groupBy**: Pros: + Fast and efficient for large datasets + Easy to use and concise syntax + Works well with complex grouping logic * Cons: + Adds external dependency (Lodash) + May not be suitable for very small datasets due to overhead * **Array.reduce**: Pros: + Native JavaScript method, no external dependencies + Simple and easy to understand code + Flexible for custom grouping logic * Cons: + Can be slow and inefficient for large datasets + Code can become complex and hard to read for deep groupings * **Object.fromEntries**: Pros: + Native JavaScript method, no external dependencies + Fast and efficient for small datasets + Simple code and easy to understand Cons: * May not be suitable for large datasets due to overhead * Limited flexibility for custom grouping logic **Considerations** When choosing an approach, consider the size of your dataset, the complexity of your grouping logic, and the trade-off between performance and simplicity. If you need a fast and efficient solution for large datasets with complex groupings, Lodash groupBy might be the best choice. For smaller datasets or simple groupings, Array.reduce or Object.fromEntries might be more suitable. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides many useful functions for tasks such as data transformation, array manipulation, and object creation. The `_groupBy` function is one of its most commonly used functions. **Special JS feature/syntax: None** There are no special JavaScript features or syntaxes used in this benchmark. **Alternatives** Other alternatives to the three approaches mentioned above include: * **Array.prototype.reduce()**: A more concise and simpler version of Array.reduce * **Array.prototype.forEach()**: While not optimized for grouping, can be used with a custom accumulator object to group data * **Vanilla JavaScript object creation methods**: Such as `Object.assign()` or `Object.create()`, which can be used to create objects from iterables However, these alternatives may have different performance characteristics and might require more code complexity.
Related benchmarks:
lodas2 groupBy vs Array.reduce
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k - test
lodash groupBy vs Array.reduce vs Object.groupBy 100k
Comments
Confirm delete:
Do you really want to delete benchmark?