Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.reduce() vs _.get() of lodash for nested objects without arrays
(version: 0)
Comparing performance of:
Array.prototype.reduce() vs _.get()
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 = 'a.b.c'; var objects = { 'a': { 'b': { 'c': 1234 } } };
Tests:
Array.prototype.reduce()
key.split('.').reduce(function(acc, cur) { return (acc || {})[cur]; }, objects);
_.get()
_.get(objects, key);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.reduce()
_.get()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.reduce()
8134479.0 Ops/sec
_.get()
5221451.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON benchmark measures the performance of two different approaches to access nested objects in JavaScript: 1. `Array.prototype.reduce()`: This method reduces an array to a single value, accumulating a result by applying a specified function against each element in the array. 2. `_get()` from Lodash library: A utility function that provides a way to safely navigate nested object property paths. The benchmark compares these two approaches for accessing nested objects without arrays. **Options compared** There are only two options being compared: 1. `Array.prototype.reduce()` 2. `_get()` from Lodash library **Pros and Cons of each approach:** 1. **`Array.prototype.reduce()`**: * Pros: + This is a built-in JavaScript method, so it's likely to be implemented efficiently. + It provides a way to iterate over an array, even if there's only one element. * Cons: + The function passed to `reduce()` must return a value that can be used as the next accumulator value. This might require some extra work for complex paths. + Using this method for simple property access can lead to unnecessary function calls and iterations. 2. **_get()` from Lodash library**: * Pros: + Specifically designed for safely navigating nested object property paths. + Reduces the risk of errors caused by incorrect path concatenation or accessing non-existent properties. * Cons: + An additional dependency is introduced, as it relies on the Lodash library. + It might not be optimized for performance, especially if the library's implementation isn't tailored to this specific use case. **Library and its purpose** The `_get()` function from Lodash provides a safe way to access nested object properties. Its primary purpose is to prevent errors that can occur when accessing properties using string concatenation or array indexing, which might lead to `undefined` values or exceptions. In the context of this benchmark, `_get()` is used to navigate the `objects` property path, which consists of multiple levels: `'a.b.c'`. **Other considerations** * In the `Script Preparation Code`, a variable `key` is defined with the value `'a.b.c'`. This will be used as the path to access in both benchmark definitions. * The `Html Preparation Code` includes a script tag that loads the Lodash library version 4.17.5. **Alternative approaches** Other alternatives for accessing nested objects might include: 1. Using string concatenation or array indexing directly, which can lead to errors and potential performance issues. 2. Implementing custom recursive functions or iterative solutions using methods like `for` loops or `forEach()`. 3. Utilizing other libraries or modules that provide similar functionality to `_get()`, such as `lodash-es` or `ramda`. However, these alternatives would likely introduce additional dependencies, complexity, and potential performance overhead compared to the two options being benchmarked.
Related benchmarks:
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
Array.prototype.slice vs Lodash drop
Comments
Confirm delete:
Do you really want to delete benchmark?