Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash groupBy vs Array.reduce with map
(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++) { if (i%2==0) { data.push({ id: i.toString() }); } else { data.push({ id: "i lol" }); } }
Tests:
Lodash
Object.values(_.groupBy(data, "id"))
Native
var resultMap = data.reduce((acc, item) => { if (!acc.has(item.id)) { acc.set(item.id, []); } acc.get(item.id).push(item); return acc; }, new Map()) Array.from(resultMap.values())
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:
11 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Browser/OS:
Chrome 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
364.0 Ops/sec
Native
191.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark and explain what it tests, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The JSON benchmark defines two test cases: 1. **Lodash**: Tests the performance of Lodash's `groupBy` function on a large dataset. * The script preparation code creates an array `data` with 100,000 elements, where every even-indexed element has an `id` property and every odd-indexed element has a string `id` value. * The HTML preparation code includes the Lodash library. 2. **Native**: Tests the performance of JavaScript's built-in `reduce`, `Map`, and `Array.from` functions to group the data by the `id` property. * The script preparation code creates an array `data` with 100,000 elements, as described above. * Note that this test case uses a library (JavaScript's standard library) rather than a third-party library. **Options Compared** The two options compared are: 1. **Lodash**: Uses the popular JavaScript utility library Lodash to group the data by the `id` property. 2. **Native**: Uses JavaScript's built-in functions to group the data by the `id` property. **Pros and Cons** * **Lodash**: + Pros: Well-tested, well-maintained, and widely used library with a large community. It provides a simple and concise way to perform grouping operations. + Cons: Adds overhead due to the inclusion of an external library. May not be suitable for projects where size is critical or where performance is a top priority. * **Native**: + Pros: No additional library overhead, which can improve performance in certain scenarios. Utilizes built-in JavaScript features, making it more predictable and maintainable. + Cons: Requires manual implementation of the grouping logic, which can be error-prone if not done correctly. **Other Considerations** * **Memory Usage**: Both tests create large datasets, but Lodash's `groupBy` function may consume more memory due to its internal data structures. * **Cache Performance**: The results may vary depending on the browser's cache and how it affects the repeated execution of the benchmark. In this case, both browsers seem to have similar cache performance. * **Optimization Opportunities**: Depending on the specific use case, there might be opportunities to optimize either approach, such as using a more efficient data structure or leveraging hardware acceleration. **Library and Purpose** In this case, the library used is Lodash, which provides a set of reusable functions for common tasks like grouping, mapping, filtering, and more. The `groupBy` function in particular is designed to take an array and return an object with keys as the unique values from the array and corresponding values as arrays of elements that have those keys. I hope this explanation helps you understand what's being tested in the provided JSON benchmark!
Related benchmarks:
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k - test
lodash groupBy vs Array.reduce vs Array.group 100k
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs Object.groupBy 100k
Comments
Confirm delete:
Do you really want to delete benchmark?