Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k more betterer
(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; var data = []; for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
Tests:
Lodash
_.groupBy(data, ({ id }) => id)
Native
data.reduce((acc, item) => { if (acc[item.id]) { acc[item.id].push(item) } else { acc[item.id] = [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:
Run details:
(Test run date:
5 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
366.5 Ops/sec
Native
699.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose** The benchmark compares the performance of two approaches to group an array of objects by a specific property: `_.groupBy` from Lodash, and the built-in `Array.reduce` method. **Options Compared** The benchmark tests two options: 1. **Lodash _.groupBy**: This approach uses the Lodash library to implement the grouping logic. It's a pre-built function that takes an array and a callback function as input, and returns a new object with grouped arrays. 2. **Native Array.reduce**: This approach uses the built-in `Array.reduce` method to achieve the same result. It iterates over the array, applying a reduction function (in this case, a simple grouping logic) to each element. **Pros and Cons of Each Approach** 1. **Lodash _.groupBy**: * Pros: High-level abstraction, easy to read and maintain, well-tested and maintained by Lodash community. * Cons: Adds an external dependency (Lodash library), may have slower overhead due to additional indirection. 2. **Native Array.reduce**: * Pros: Built-in, lightweight, no additional dependencies required. * Cons: Requires manual implementation of grouping logic, which can lead to errors and maintenance issues. **Library** The Lodash library is a popular JavaScript utility library that provides various helper functions for tasks like array manipulation, string normalization, and more. In this benchmark, _.groupBy is used as a pre-built function to simplify the testing process. **Special JS Feature or Syntax** There are no special features or syntax mentioned in the benchmark definition or test cases. The focus is on comparing the performance of two approaches, with no specific optimizations or edge cases introduced. **Other Alternatives** If you need to group arrays by a property using a different approach, some alternatives could be: 1. **Array.prototype.forEach**: This method can be used in conjunction with the `this` keyword and an object to iterate over the array. 2. **Array.prototype.map**: This method can be used to transform each element of the array into a new array, which can then be grouped using other methods. Keep in mind that these alternatives may have performance characteristics similar to or different from the Lodash _.groupBy and Native Array.reduce approaches tested in this benchmark.
Related benchmarks:
lodash groupBy vs Array.reduce (2)
lodas2 groupBy vs Array.reduce
lodash groupBy vs Array.reduce on million items
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?