Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array to object by key with lodash
(version: 0)
Comparing performance of:
Array.prototype.reduce() vs _.reduce() vs _.keyBy()
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
var object = { 'a': { 'key': 'a', 'foo': 123123, 'bar': 123, 'fobar': 456 } }; var array = [ { 'key': 'b', 'foo': 67563, 'bar': 6345, 'fobar': 3425 }, { 'key': 'c', 'foo': 34532, 'bar': 123412, 'fobar': 534532 }, { 'key': 'd', 'foo': 1234321, 'bar': 435234, 'fobar': 346457 }, { 'key': 'e', 'foo': 23523, 'bar': 124325, 'fobar': 2134235 }, { 'key': 'f', 'foo': 1235213, 'bar': 346346, 'fobar': 213423 } ];
Tests:
Array.prototype.reduce()
array.reduce(function(acc, cur) { acc[cur.key] = cur; return acc; }, Object.assign({}, object));
_.reduce()
_.reduce(array, function(acc, cur) { acc[cur.key] = cur; return acc; }, Object.assign({}, object));
_.keyBy()
Object.assign({}, object, _.keyBy(array, 'key'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.reduce()
_.reduce()
_.keyBy()
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 explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The test case is designed to measure the performance of three different methods for converting an array into an object by key: 1. `Array.prototype.reduce()` 2. `_` (Lodash) function `.reduce()` 3. `_` (Lodash) function `.keyBy()` with `Object.assign()` as the accumulator. **Options Compared** The three options are being compared in terms of execution time, which is represented by the "ExecutionsPerSecond" value. * `Array.prototype.reduce()`: This method uses a simple loop to iterate through the array and assign each object to an empty object. * `_` (Lodash) function `.reduce()`: This method uses Lodash's `reduce()` function, which is optimized for performance. It takes advantage of the iterator protocol and lazy evaluation to reduce memory allocation. * `_` (Lodash) function `.keyBy()`: This method uses Lodash's `keyBy()` function, which creates a new object with the original array elements as values. The resulting object has the same structure as the original array. **Pros and Cons** Here are some pros and cons of each approach: * `Array.prototype.reduce()`: + Pros: Simple, lightweight implementation. + Cons: Requires manual memory management, which can lead to performance issues with large datasets. + Memory usage: High due to the need to create a new object for each iteration. * `_` (Lodash) function `.reduce()`: + Pros: Optimized performance, takes advantage of iterator protocol and lazy evaluation, reduces memory allocation. + Cons: Requires an external library (Lodash). + Memory usage: Lower compared to `Array.prototype.reduce()` due to Lodash's optimized implementation. * `_` (Lodash) function `.keyBy()`: + Pros: Provides a convenient way to create an object from an array by key, eliminates the need for manual memory management. + Cons: Requires an external library (Lodash), has a slightly higher memory overhead compared to `.reduce()`. **Other Considerations** * **Memory usage**: The three options have different memory usage profiles. `Array.prototype.reduce()` has the highest memory usage due to the need to create new objects for each iteration, while `_` (Lodash) function `.keyBy()` has a lower memory overhead. * **Performance**: The order of performance is `.reduce() > .keyBy() > reduce()`, with Lodash's optimized implementation providing the best performance. **Alternatives** If you're looking for alternatives to these options, here are some alternatives: * Use `Object.fromEntries()` (ES6+) instead of `Array.prototype.reduce()` or `_` (Lodash) function `.reduce()`. This method is more concise and has a lower memory overhead. * Consider using other libraries like [lodash-es](https://github.com/lodash/lodash-es) or [ramda](https://ramdajs.com/) for functional programming in JavaScript. These libraries provide optimized implementations for various operations, including array reduction. Keep in mind that the choice of implementation depends on your specific use case and performance requirements.
Related benchmarks:
Array.prototype.reduce() vs _.get() of lodash for nested objects without arrays
sdfgsdfg
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
Object Length vs Lodash Size 100k
Object values Length vs Lodash Size 100k
Comments
Confirm delete:
Do you really want to delete benchmark?