Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce 100k better 2
(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) => { const groupValue = (item['id'] ?? '').toString(); const results = acc[groupValue] ?? []; results.push(item); acc[groupValue] = results; 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
905.4 Ops/sec
Native
378.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and its test cases to understand what is being tested. **Benchmark Definition** The benchmark is designed to compare two approaches for grouping an array of objects based on a specific property: `id`. The two approaches are: 1. **Lodash**: Using the `_groupBy` function from the Lodash library. 2. **Native**: Using the `reduce` method native to JavaScript. **Lodash** Lodash is a popular JavaScript utility library that provides various functions for common tasks, including data manipulation. In this case, the `_groupBy` function takes two arguments: the array of objects (`data`) and a callback function (`({ id }) => id`). The callback function returns the value used for grouping, in this case, just the `id` property. The Lodash implementation is likely to be faster because it's implemented in C++ (by Facebook) and then wrapped in JavaScript. This compilation step can provide significant performance benefits compared to interpreting JavaScript code directly on the client-side or server-side. Pros of using Lodash: * Faster execution due to native compilation * Easier to read and maintain, as the implementation is separate from the original code Cons of using Lodash: * Additional dependencies, which may not be desired in some projects * May introduce additional overhead for small datasets **Native** The Native approach uses the `reduce` method to group the array of objects. The callback function takes two arguments: the accumulator (`acc`) and the current item (`item`). It returns a new object with the updated grouping value. Pros of using Native: * No dependencies or added complexity * Can be more efficient for small datasets, as it doesn't require compilation Cons of using Native: * Requires manual handling of edge cases (e.g., empty groups) * More verbose and difficult to read compared to Lodash implementation **Other Considerations** When choosing between these approaches, consider the trade-off between performance and maintainability. If you prioritize speed and are willing to add a dependency, Lodash might be a better choice. However, if you prefer to avoid additional dependencies and are comfortable with more verbose code, Native could be a viable option. **Library (Lodash)** Lodash is a popular JavaScript utility library developed by Facebook. It provides various functions for common tasks, such as data manipulation, string manipulation, and functional programming techniques. Lodash is widely used in the industry and is often included in project dependencies. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The implementation relies on standard JavaScript methods and libraries (Lodash).
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
Comments
Confirm delete:
Do you really want to delete benchmark?