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 _.reduce() with _.get() 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 key = 'key'; 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()
// This code can't find dot-notations key and nested property. array.reduce(function(acc, cur) { acc[cur[key]] = cur; return acc; }, Object.assign({}, object));
_.reduce()
// This code can't find dot-notations key and nested property. _.reduce(array, function(acc, cur) { acc[cur[key]] = cur; return acc; }, Object.assign({}, object));
_.reduce() with _.get()
_.reduce(array, function(acc, cur) { acc[_.get(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 (4)
Previous results
Fork
Test case name
Result
Array.prototype.reduce()
_.reduce()
_.reduce() with _.get()
_.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):
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition is the code that defines the test case. In this case, there are four test cases: 1. `Array.prototype.reduce()`: This function iterates over an array and executes a user-provided function on each element. 2. `_.reduce()`: This is a function from the Lodash library that performs the same operation as `Array.prototype.reduce()`. 3. `_.reduce()` with `_get()`: This uses the `_get` function from Lodash to access nested properties in the array elements. 4. `Object.assign({}, object, _.keyBy(array, key))`: This uses the `keyBy` function from Lodash to create a new object where each key is a value from the original array. **Comparing Approaches** The four test cases compare different approaches to merge an object with an array: 1. **Native JavaScript**: The first two test cases use native JavaScript functions (`Array.prototype.reduce()` and `_.reduce()`) to perform the merge. 2. **Lodash `keyBy`**: The third test case uses Lodash's `keyBy` function to create a new object where each key is a value from the original array. 3. **Lodash `_get` + `Object.assign`**: The fourth test case uses Lodash's `_get` function to access nested properties in the array elements and then uses `Object.assign` to merge the objects. **Pros and Cons** Here are some pros and cons of each approach: 1. **Native JavaScript**: * Pros: Fast, lightweight, and widely supported. * Cons: Can be more error-prone due to lack of built-in support for nested properties. 2. **Lodash `keyBy`**: * Pros: Easy to use, well-documented, and provides a convenient way to access nested properties. * Cons: Adds external dependency (Lodash) and can be slower than native JavaScript. 3. **Lodash `_get` + `Object.assign`**: * Pros: Provides fine-grained control over accessing nested properties and allows for more flexibility in merging objects. * Cons: Can be slower due to the use of `_get`, adds external dependency (Lodash), and requires more code. **Other Considerations** When choosing an approach, consider the following: * **Performance**: Native JavaScript might be faster due to lack of overhead from external libraries. However, Lodash's optimized implementations can provide better performance in some cases. * **Code Readability**: If you're writing a simple script or prototype, native JavaScript might be sufficient. For more complex applications, using Lodash's functions can make the code more readable and maintainable. * **Maintenance**: Adding external dependencies like Lodash can make maintenance more difficult, especially if you need to update the library in the future. **Alternatives** If you don't want to use native JavaScript or external libraries like Lodash, consider these alternatives: 1. **Use a different merging function**: You can create your own custom merging function that iterates over the array and objects. 2. **Use a library with similar functionality**: Other libraries, such as underscore.js (another popular utility library) or Ramda (a functional programming library), might provide similar functions for merging arrays and objects. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash merge vs object.assign vs spread with arrays
lodash merge vs object.assign vs spread vs manual forEach for array merging
lodash merge vs object.assign vs spread with deep values
lodash vs spread w/array
Merge objects with spread vs foreach & new
Comments
Confirm delete:
Do you really want to delete benchmark?