Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs Property dot notation nested vs optional chaining
(version: 0)
Comparing performance of:
Lodash get vs Native naive vs Guarded native vs Optional chaining
Created:
4 years ago
by:
Guest
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'}};
Tests:
Lodash get
_.get(person, 'details.eyes');
Native naive
person.details.eyes
Guarded native
person && person.details && person.details.eyes
Optional chaining
person?.details?.eyes
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
Optional chaining
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark tests four different ways to access the `eyes` property of an object within an object: 1. **Lodash `.get()`**: Using Lodash's `_.get()` method to safely navigate the nested object. 2. **Property dot notation nested**: Accessing the property using dot notation (`person.details.eyes`). 3. **Optional chaining**: Using the optional chaining operator (`?.`) to access the property. **Script Preparation Code:** The script defines a simple JavaScript object `person` with nested properties: ```javascript var person = {name: 'Frederick', lastName: 'Corcino Alejo', details: {eyes: 'brown'}} ``` This object is used as the test subject for all benchmark cases. **Html Preparation Code:** The HTML code includes a script tag that loads Lodash version 4.16.0 from a CDN: ```html <script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script> ``` This ensures that the Lodash library is available for the benchmark cases. **Individual Test Cases:** The test cases are as follows: 1. **Lodash `.get()`**: `_.get(person, 'details.eyes');` 2. **Native naive**: `person.details.eyes` 3. **Guarded native**: `person && person.details && person.details.eyes` 4. **Optional chaining**: `person?.details?.eyes` **Library:** * Lodash is a popular JavaScript utility library that provides various functions for tasks such as string manipulation, array operations, and object manipulation. **Special JS Feature/Syntax:** * Optional chaining (`?.`) was introduced in ECMAScript 2020 (ES11) as a new way to access nested properties safely. It allows you to avoid null pointer exceptions by only accessing the property if it exists. * The `&&` operator with guard checks is also used in some older JavaScript implementations, but it's not as safe or expressive as optional chaining. **Pros and Cons:** 1. **Lodash `.get()`**: Pros: + Provides a safe way to access nested properties. * Cons: + Adds extra overhead due to the function call. + May be slower than native approaches. 2. **Property dot notation nested**: Pros: * Fast and efficient (no function call or overhead). * Easy to read and understand. * Cons: + Can throw null pointer exceptions if any of the properties don't exist. 3. **Optional chaining**: Pros: + Provides a safe way to access nested properties while avoiding null pointer exceptions. + More expressive and concise than guard checks. * Cons: + May be slower than native approaches due to the optional operator (`?.`). 4. **Guarded native**: Pros: * Fast and efficient (no function call or overhead). * Easy to read and understand. * Cons: + Requires careful handling of null and undefined values. **Other Alternatives:** If you want to avoid using Lodash, you can implement a similar function using plain JavaScript: ```javascript function getNestedProperty(obj, path) { const parts = path.split('.'); let currentObj = obj; for (const part of parts) { if (!currentObj[part]) return undefined; currentObj = currentObj[part]; } return currentObj; } ``` This function uses a loop to navigate the object graph and returns the value of the nested property. However, it doesn't provide the same level of safety or expressiveness as Lodash's `_.get()` method. Keep in mind that this benchmark is specific to the JavaScript engine used by MeasureThat.net and may not be representative of all browsers or environments.
Related benchmarks:
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 longer path
Comments
Confirm delete:
Do you really want to delete benchmark?