Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs Property dot notation deeply nested
(version: 0)
Comparing performance of:
Lodash get vs Native naive vs Guarded native vs Shimmed get.
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var person = {name: 'Frederick', lastName: 'Corcino Alejo', details: {eyes: 'brown', hobbies: ['skiing', 'cooking']}};
Tests:
Lodash get
_.get(person, 'details.hobbies.1');
Native naive
person.details.hobbies[1]
Guarded native
person && person.details && person.details.hobbies && person.details.hobbies.length && person.details.hobbies[1]
Shimmed get.
const get = (obj, path, defaultValue) => { const result = String.prototype.split.call(path, /[,[\].]+?/) .filter(Boolean) .reduce((res, key) => (res !== null && res !== undefined) ? res[key] : res, obj); return (result === undefined || result === obj) ? defaultValue : result; }; get(person, 'details.hobbies.1');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash get
Native naive
Guarded native
Shimmed get.
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 dive into the world of JavaScript microbenchmarks! **Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark tests four approaches to accessing nested properties in an object: Lodash's `get` function, native naive approach, guarded native approach, and a custom shimmed `get` implementation. **Options Compared** The test cases compare the performance of: 1. **Lodash.get**: A utility function from the Lodash library that allows accessing nested properties in an object. 2. **Native Naive**: Directly accessing nested properties using dot notation (`person.details.hobbies[1]`). 3. **Guarded Native**: Using a guarded approach to ensure the existence of intermediate objects before attempting to access them (`person && person.details && person.details.hobbies && ...`). 4. **Shimmed Get**: A custom implementation that splits the property path into segments, filters out unnecessary parts, and reduces the result using `String.prototype.split()`. **Pros and Cons** Here's a brief overview of each approach: 1. **Lodash.get**: * Pros: Convenient and expressive API, handles errors by default. * Cons: External dependency (Lodash), may have overhead due to function call. 2. **Native Naive**: * Pros: Lightweight, no external dependencies. * Cons: May result in null pointer exceptions or errors if intermediate objects are missing. 3. **Guarded Native**: * Pros: Safely handles non-existent intermediate objects by short-circuiting the evaluation. * Cons: May incur overhead due to additional checks and conditional statements. 4. **Shimmed Get**: * Pros: Customizable, can be optimized for specific use cases. * Cons: Requires manual implementation and maintenance. **Library** The Lodash library is a popular JavaScript utility library that provides various functions for working with data structures, including arrays, objects, and more. `get` is one of the many useful functions provided by Lodash. **Special JS Feature/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax, but it's worth noting that the custom shimmed `get` implementation uses `String.prototype.split()` to split the property path into segments, which is a relatively recent feature introduced in ECMAScript 2015 (ES6). **Alternative Approaches** If you're looking for alternative approaches to accessing nested properties in an object, consider: 1. **Use of Object.keys() and bracket notation**: Accessing nested properties using `Object.keys()` and the bracket notation (`person.details.hobbies[1]`). 2. **Using a recursive function**: Implementing a custom recursive function to traverse the property path. 3. **Utilizing modern JavaScript features**: Leveraging features like `Array.prototype.find()` or `String.prototype.includes()` for more concise and expressive access. Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case, performance requirements, and coding style preferences.
Related benchmarks:
Lodash.get vs Property dot notation
Lodash.get vs Property dot notation with sanity check
Lodash.get vs Property dot notation - 2 deep
Lodash.get vs Property dot notation @movlan
Lodash.get vs Property dot notation with ?
Comments
Confirm delete:
Do you really want to delete benchmark?