Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs reduce Map()
(version: 0)
Comparing performance of:
Lodash vs Foreach Map
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 }); }
Tests:
Lodash
_.groupBy(data, ({ id }) => id)
Foreach Map
const test = new Map(); data.forEach((item) => { test.set(item.id, item) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Foreach Map
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):
Let's break down the provided benchmark definition and test cases. **Overview** The benchmark is designed to compare the performance of two approaches: using Lodash's `groupBy` function and using a manual approach with a `Map` data structure. The goal is to determine which method is faster for grouping an array of objects by their `id` property. **Script Preparation Code** The script preparation code creates an array of 100,000 objects with an `id` property ranging from 0 to 99,999. This is done using a simple `for` loop and the `push` method. **Html Preparation Code** The HTML preparation code includes a reference to Lodash's `lodash.min.js` library, which will be used for the Lodash-based test case. **Test Cases** ### Test Case 1: Lodash ```javascript _.groupBy(data, ({ id }) => id) ``` This line uses Lodash's `groupBy` function to group the `data` array by the `id` property. The function takes a predicate as an argument, which returns the value of the `id` property for each object in the array. **Pros:** * Easy to read and maintain * Optimized for performance by using Lodash's internal implementation **Cons:** * Requires an additional library dependency (Lodash) * May have overhead due to the external library ### Test Case 2: Foreach Map ```javascript const test = new Map(); data.forEach((item) => { test.set(item.id, item); }) ``` This code creates a new `Map` data structure and iterates over the `data` array using the `forEach` method. For each object in the array, it sets the `id` property as the key and the entire object as the value. **Pros:** * No additional library dependency required * Uses built-in JavaScript data structures **Cons:** * May be slower due to manual iteration and mapping * Requires more code and maintenance effort **Library: Lodash's `groupBy` function** The `groupBy` function is a utility function from the Lodash library that groups an array of objects by a specified property. It returns a new object with the grouped values. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntax used in these test cases. **Other Alternatives** * Using other group-by functions, such as `Array.prototype.reduce()` or `Array.prototype.forEach()`, might be viable alternatives. * Implementing a custom grouping function using recursion or other techniques could also be explored. In conclusion, the benchmark compares two approaches for grouping an array of objects by their `id` property. Lodash's `groupBy` function provides an optimized and easy-to-use solution, while the manual `Map` approach offers a lightweight alternative with no additional library dependency required.
Related benchmarks:
Loop over object: lodash vs Object.entries fork by d9k 2
Loop over object: lodash vs Object.entries fork by d9k 3
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
lodash entries vs object entries
Comments
Confirm delete:
Do you really want to delete benchmark?