Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Group by
(version: 0)
Comparing performance of:
Pure JS vs Lodash prop vs Lodash func
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 inventory = [{ name: "asparagus", type: "vegetables", quantity: 5 }, { name: "bananas", type: "fruit", quantity: 0 }, { name: "goat", type: "meat", quantity: 23 }, { name: "cherries", type: "fruit", quantity: 5 }, { name: "fish", type: "meat", quantity: 22 }, ];
Tests:
Pure JS
const result = Object.groupBy(inventory, ({ type }) => type);
Lodash prop
_.groupBy(inventory, 'type');
Lodash func
_.groupBy(inventory, ({ type }) => type);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Pure JS
Lodash prop
Lodash func
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):
I'll break down the provided benchmark and its options, explaining what's tested, pros and cons of each approach, and other considerations. **Benchmark Overview** The provided benchmark measures the performance of different ways to group an array of objects by a common property in JavaScript. The test data consists of an inventory array with multiple objects, each having a name, type (vegetables, fruit, or meat), and quantity properties. **Script Preparation Code** The script preparation code provides the initial inventory array: ```javascript var inventory = [ { name: "asparagus", type: "vegetables", quantity: 5 }, { name: "bananas", type: "fruit", quantity: 0 }, { name: "goat", type: "meat", quantity: 23 }, { name: "cherries", type: "fruit", quantity: 5 }, { name: "fish", type: "meat", quantity: 22 } ]; ``` **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library, which is used in some of the benchmark options: ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` **Benchmark Definition Options** There are three benchmark definition options: 1. **"Pure JS"**: Uses only built-in JavaScript functions to group the array. 2. **"Lodash prop"**: Uses Lodash's `groupBy` function with a property accessor (in this case, `'type'`) to group the array. 3. **"Lodash func"**: Uses Lodash's `groupBy` function with a custom function as the key function to group the array. **Pros and Cons of Each Approach** 1. **"Pure JS"**: * Pros: Only depends on built-in JavaScript functions, no additional library dependencies. * Cons: Can be slower due to the need to implement grouping logic manually. 2. **"Lodash prop"**: * Pros: Uses a well-tested and optimized library function, reduces implementation complexity. * Cons: Requires an external library dependency. 3. **"Lodash func"**: * Pros: Provides more control over the grouping logic using a custom function. * Cons: Requires knowledge of Lodash's `groupBy` function and adds an additional library dependency. **Other Considerations** * The benchmark measures the performance of each option, so it's essential to consider factors like code readability, maintainability, and performance when choosing an approach. * If you're not familiar with Lodash or prefer not to use external libraries, "Pure JS" might be a good starting point. However, if you need more control over the grouping logic or want to leverage a well-tested library function, "Lodash func" or "Lodash prop" might be better options. * The benchmark results show that all three options have similar performance characteristics on this specific test case. **Library Explanation** The Lodash `groupBy` function is a utility function that takes two arguments: 1. The array to group (`inventory`) 2. A key function or property accessor to determine the group keys (in this case, `'type'`) The `groupBy` function returns an object with each group as a key, and an array of objects belonging to that group. In the benchmark options, Lodash's `groupBy` function is used differently: * "Lodash prop" uses a property accessor (`'type'`) as the key. * "Lodash func" uses a custom function `(type) => type` as the key.
Related benchmarks:
length -1 vs last
IndexOf vs Includes vs lodash includes2
lodash vs ES6 sortby native fix
lodash includes vs has
Comments
Confirm delete:
Do you really want to delete benchmark?