Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash test - keyBy
(version: 0)
Lodash test - keyBy
Comparing performance of:
Lodash vs Vanilla JS
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="lodash.js"></script>
Script Preparation code:
var array = [ { 'name': 'lim', 'age': 26, 'key': 1 }, { 'name': 'kim', 'age': 28, 'key': 2 }, { 'name': 'choi', 'age': 32, 'key': 1 }, { 'name': 'park', 'age': 21, 'key': 3 } ];
Tests:
Lodash
_.keyBy(array, 'key');
Vanilla JS
array.reduce((pre, cur) => { pre[cur.key] = pre[cur.key] || []; pre[cur.key].push(cur); return pre; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Vanilla JS
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 benchmarking test case. **Benchmark Definition** The benchmark is designed to measure the performance of two approaches for creating an object with grouped data: using Lodash's `keyBy` function and using a vanilla JavaScript approach with the `reduce` method. **Options Compared** 1. **Lodash's keyBy**: This option uses the `_.keyBy` function from the Lodash library, which takes an array of objects and returns a new object where each key is a value from the original array's 'key' property. 2. **Vanilla JavaScript with reduce**: This option uses the `reduce` method to create an object with grouped data. The idea is to iterate over the array, for each element, create a new property in the accumulator object if it doesn't exist, and then push the current element into that property. **Pros and Cons of Each Approach** 1. **Lodash's keyBy** * Pros: + Concise and readable code + Easy to understand and use, especially for developers familiar with Lodash * Cons: + Requires including an additional library (Lodash) in the benchmark + May have performance overhead due to the library's existence 2. **Vanilla JavaScript with reduce** * Pros: + No external dependencies required + Can be optimized for performance by using techniques like memoization or caching * Cons: + Code can be more complex and harder to understand, especially for developers without experience with `reduce` + May have a slight performance penalty due to the complexity of the code **Library: Lodash** Lodash is a popular JavaScript library that provides various utility functions, including `keyBy`, which helps with data transformation and manipulation. In this benchmark, `_.keyBy` is used to create an object with grouped data. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntaxes used in these benchmark tests. **Other Alternatives** 1. **Using `Array.prototype.reduce()`**: This approach would be similar to the vanilla JavaScript with reduce approach, but it's a built-in method and might be slightly faster due to its native implementation. 2. **Using `Array.prototype.forEach()` and object initialization**: Another possible approach would be to use `forEach` to iterate over the array and initialize an object with grouped data using object literal syntax. In conclusion, this benchmark provides a simple and clear comparison between two approaches for creating an object with grouped data: Lodash's `keyBy` function and vanilla JavaScript with the `reduce` method. It helps developers understand the performance characteristics of each approach and choose the most suitable one for their use case.
Related benchmarks:
lodash vs es6 in find method
lodash vs es6 in map method
lodash vs es6 in some method
Lodash test - find
Comments
Confirm delete:
Do you really want to delete benchmark?