Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
merge object by key from array 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'd be happy to help you understand what's being tested on MeasureThat.net. **Benchmark Overview** The provided benchmark tests the performance of three different approaches to merge an object by key from an array: using `Array.prototype.reduce()`, `_` (Lodash) library functions, and another Lodash function `_.keyBy()`. **Approach 1: Array.prototype.reduce()** This approach uses the built-in `reduce()` method on the `Array.prototype`. The `reduce()` method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single output value. In this case, we're using the function to iterate over the array, merge the objects by key, and return the resulting object. **Pros:** * No external dependencies or libraries are required. * This approach is likely to be implemented in native code, which can result in faster execution times. **Cons:** * The implementation may vary between browsers and versions, as `reduce()` is a built-in method that's not part of the ECMAScript standard. * The performance may degrade if the array is very large or if the objects being merged are complex. **Approach 2: _.reduce() (Lodash)** This approach uses the `_` library to call its own implementation of `reduce()`. The Lodash `reduce()` function is a wrapper around the native `Array.prototype.reduce()` method, which provides additional features and error handling. **Pros:** * The implementation is consistent across browsers and versions. * The Lodash library provides additional features, such as error handling and support for other data structures (like objects). **Cons:** * An external dependency is required to use the Lodash library. * The performance may be slower compared to a native `reduce()` implementation. **Approach 3: _.keyBy() (Lodash)** This approach uses the `_` library's `keyBy()` function, which creates a map of each element in the array by its key. We then use this map to merge the objects by key. **Pros:** * The implementation is consistent across browsers and versions. * The Lodash library provides additional features, such as support for other data structures (like objects). **Cons:** * An external dependency is required to use the Lodash library. * The performance may be slower compared to a native `reduce()` implementation. **Other Considerations** * The test results show that the order of operations matters. In the case of `_` and `_.keyBy()`, the `Object.assign({}, object)` call creates a new object, which is then merged with the result of the `_.reduce()` or `_.keyBy()` function. This can lead to additional overhead compared to a native `reduce()` implementation. * The test results also show that the browser and device platform can affect performance. In this case, Chrome 64 on Mac OS X 10.13.3 appears to be the fastest performer. **Alternatives** There are other approaches to merge objects by key from an array, such as using a `Map` or a `Reduce()` implementation with a more complex function. However, these alternatives may not be suitable for this specific use case, and the Lodash library is often used due to its flexibility and consistency across browsers and versions. In summary, each approach has its pros and cons, and the choice of which one to use depends on the specific requirements of your project.
Related benchmarks:
object to array with lodash
merge object by key from array with lodash
merge object by key from array with lodash
merge object by key from array with lodash
Comments
Confirm delete:
Do you really want to delete benchmark?