Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k fair
(version: 0)
Comparing performance of:
Lodash vs Native vs groupByReduce vs groupByReduceBetter vs groupByHasOwnProperty vs groupByMapBetter
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 }); } function groupByReduce(arr, iteratee) { return arr.reduce((acc, v) => { const k = iteratee(v); (acc[k] || (acc[k] = [])).push(v) return acc; }, {}) } function groupByReduceBetter(arr, iteratee) { return arr.reduce((acc, v) => { const k = iteratee(v); if(acc[k]) { acc[k].push(v); } else { acc[k] = [v]; } return acc; }, {}) } function groupByHasOwnProperty(arr, iteratee) { return arr.reduce((acc, v) => { const k = iteratee(v); if(acc.hasOwnProperty(k)) { acc[k].push(v); } else { acc[k] = [v]; } return acc; }, {}) } function groupByMap(arr, iteratee) { const m = new Map(); for (const v of arr) { const k = iteratee(v); const temp = m.get(k) ?? []; temp.push(v); m.set(k, temp) } return m; } function groupByMapBetter(arr, iteratee) { const m = new Map(); for (let length = arr.length, i = 0; i < length; ++i) { const k = iteratee(arr[i]); let temp = m.get(k); if (temp) { temp.push(arr[i]); } else { temp = [arr[i]] } m.set(k, temp) } return m; } var iteratee = ({ id }) => id
Tests:
Lodash
_.groupBy(data, iteratee)
Native
data.reduce((acc, item) => { if(acc[item.id]) { acc[item.id].push(item); } else { acc[item.id] = [item]; } return acc; }, {})
groupByReduce
groupByReduce(data, iteratee)
groupByReduceBetter
groupByReduceBetter(data, iteratee)
groupByHasOwnProperty
groupByHasOwnProperty(data, iteratee)
groupByMapBetter
groupByMapBetter(data, iteratee)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Lodash
Native
groupByReduce
groupByReduceBetter
groupByHasOwnProperty
groupByMapBetter
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):
I'll break down the provided JSON data and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark tests four different approaches to grouping an array of objects by a specific property: 1. `groupByReduce(data, iteratee)`: uses the reduce() method on the original array 2. `groupByReduceBetter(data, iteratee)`: improves upon the previous approach by avoiding unnecessary concatenation 3. `groupByHasOwnProperty(data, iteratee)`: uses the hasOwnProperty() method to check if a property exists in an object 4. `groupByMapBetter(data, iteratee)`: uses a Map data structure to group the array **Test Case Explanations** Each test case uses the provided `iteratee` function, which takes an object and returns its `id` property. * `Lodash`: uses the `_groupBy()` function from the Lodash library * `Native`: uses a custom implementation of the grouping logic using reduce() * `groupByReduce`, `groupByReduceBetter`, `groupByHasOwnProperty`, `groupByMapBetter`: implement the grouping logic themselves, with slight variations in approach **Comparison** The comparison is between these four approaches: * Speed: The test measures how many executions per second each approach can perform on a large dataset (100,000 objects) * Memory usage: Not explicitly tested, but `groupByReduceBetter` and `groupByMapBetter` may use less memory due to their more efficient data structures * Readability and maintainability: Not directly comparable, as each implementation has its own strengths and weaknesses **Pros and Cons** Here's a brief summary: 1. **Native**: Simple, readable, but potentially slower due to the overhead of reduce() 2. **groupByReduce**: Straightforward, but may lead to performance issues with large datasets 3. **groupByHasOwnProperty**: May introduce unnecessary overhead due to the hasOwnProperty() method call 4. **groupByMapBetter**: Efficient and memory-friendly, but may require more setup and understanding of Map data structures **Lodash**: The Lodash implementation is likely to be the fastest, as it's optimized for performance and uses specialized libraries. However, this comes at the cost of dependency on an external library. The `Native` approach is a good starting point for those familiar with reduce(), but may require optimization for large datasets. `groupByReduceBetter` and `groupByMapBetter` are more efficient, but their trade-offs in readability and maintainability should be considered. Ultimately, the choice of implementation depends on the specific requirements of your project, including performance, memory usage, and development ease.
Related benchmarks:
lodash groupBy vs Array.reduce (1mln)
lodash groupBy vs Array.reduce vs Array.group 100k
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs simple for loop
lodash groupBy vs Array.reduce group by 100k
Comments
Confirm delete:
Do you really want to delete benchmark?