Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce vs simple for loop
(version: 0)
Comparing performance of: Lodash vs Native reduce vs native for loop for grouping
Comparing performance of:
lodash groupby vs native reduce vs group by simple for loop
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; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
lodash groupby
_.groupBy(data, ({ id }) => id)
native reduce
const data_reduced = data.reduce((acc, item) => { if (item.id in acc) { acc[item.id].push(item) } else { acc[item.id] = [item] } return acc; }, {})
group by simple for loop
const data_group_by_for_loop = {} for (let i = 0; i < data.length; i++) { const item = data[i]; if (item.id in data_group_by_for_loop) { data_group_by_for_loop[item.id].push(item) } else { data_group_by_for_loop[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
native reduce
group by simple for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash groupby
68.1 Ops/sec
native reduce
114.7 Ops/sec
group by simple for loop
38.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance is essential in software development, and Benchmarking is an excellent way to compare the efficiency of different approaches. The provided benchmark measures the performance of three approaches for grouping data: 1. **Lodash's `groupBy` function**: This function takes an array of objects and a callback function that extracts a property from each object. It returns a new array with groups of objects based on the extracted property. 2. **Native JavaScript `reduce` method**: This is a built-in method for reducing arrays to single values by applying a function to each element in the array. For grouping, it's used to accumulate an object where the keys are the groupers and the values are arrays of grouped elements. 3. **Simple `for loop` approach**: A basic approach where we iterate over the data array and manually create groups based on a certain condition (in this case, the `id` property). **Options comparison:** * **Lodash's `groupBy` function vs Native JavaScript `reduce` method**: * **Pros of Lodash's `groupBy`**: Easier to read, maintainable code; It handles edge cases like empty groups and missing keys for objects. * **Cons of Lodash's `groupBy`**: Additional dependency on Lodash library; May be slower due to the overhead of a function call. * **Native JavaScript `reduce` method vs Simple `for loop` approach**: * **Pros of Native JavaScript `reduce`**: Built-in and efficient, handles large datasets with ease; No additional dependencies required. * **Cons of Native JavaScript `reduce`**: Can be more complex to read and maintain for beginners; May require extra effort to handle edge cases. **Library description:** * Lodash is a popular JavaScript utility library that provides various functions for common tasks such as string manipulation, array and object transformations, and event handling. The `groupBy` function in this benchmark is used to group data by a specified property. **Special JS feature or syntax:** In the provided benchmark, no special JavaScript features or syntax are used beyond what's typically available in modern browsers. **Benchmark preparation code explanation:** The script preparation code creates an array of objects with unique `id` properties and pushes them into the `data` array. This is done to simulate a scenario where we're grouping a large dataset by a certain property. The HTML preparation code includes the Lodash library, which is required for the `groupBy` function in the benchmark. **Alternatives:** For comparing performance of data grouping approaches, other alternatives could include: * Using Node.js and its built-in modules (e.g., `fs`, `path`) to simulate different environments. * Utilizing a more efficient data structure like a trie or a binary search tree for grouping. * Implementing the grouping logic in a functional programming style using languages like Haskell or Scala.
Related benchmarks:
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce vs Array.group 100k
lodash groupBy vs Array.reduce 100k with array push
lodash groupBy vs Array.reduce vs Object.groupBy 100k
Comments
Confirm delete:
Do you really want to delete benchmark?