Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs Property dot notation with ?
(version: 0)
Comparing performance of:
Lodash get vs Native
Created:
2 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'};
Tests:
Lodash get
_.get(person, 'name');
Native
person?.name
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash get
Native
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash get
40952368.0 Ops/sec
Native
154695408.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Overview** The benchmark compares two approaches to access a nested property in an object: Lodash's `_.get` method and native JavaScript syntax using optional chaining (`?.`). **Options Compared** 1. **Lodash's `_.get` method**: This is a utility function provided by the Lodash library, which helps with accessing nested properties in objects. 2. **Native JavaScript syntax (`.property ?`)**: This approach uses the optional chaining operator (`?.`) to access the nested property. **Pros and Cons** 1. **Lodash's `_.get` method**: * Pros: + Provides a more explicit way of accessing nested properties, which can improve code readability. + Can be used with any object that supports dot notation. * Cons: + Adds an external dependency (Lodash library) to the project. + May have performance implications due to the additional function call. 2. **Native JavaScript syntax (.property ?)**: * Pros: + Eliminates the need for an external library, reducing dependencies and potential performance overhead. + Can provide better performance since it's a built-in operator. * Cons: + Requires support for optional chaining in the browser or environment being tested. **Special JavaScript Feature** The benchmark uses the **optional chaining operator (`?.`)**, which is a relatively recent feature introduced in ECMAScript 2020 (ES12). This operator allows you to access nested properties without throwing an error if they don't exist. The syntax `person?.name` is used to check if `person` has a `name` property before trying to access it. **Library Used** The benchmark uses the Lodash library, which provides various utility functions for working with objects, arrays, and other data structures. In this specific case, the `_.get` method is used to access nested properties. **Other Considerations** When deciding between these two approaches, consider the following factors: * Performance: If you need to optimize performance, native JavaScript syntax using optional chaining might be a better choice. * Code readability and maintainability: Lodash's `_.get` method can provide more explicit code for accessing nested properties, making it easier to read and understand. * Library dependencies: Depending on your project requirements, eliminating external libraries like Lodash can simplify the build process or reduce dependencies. **Alternatives** Other alternatives for accessing nested properties include: 1. **Brackets (`[]`) notation**: `person['name']` is a more traditional way of accessing properties in JavaScript. 2. **Property access with dot notation**: `person.name` 3. **Using `in` operator**: `if ('name' in person)` can be used to check if an object has a property. However, these alternatives might not provide the same level of readability or explicitness as Lodash's `_.get` method or native JavaScript syntax with optional chaining.
Related benchmarks:
Lodash.get vs Property dot notation
Lodash.get vs Property dot notation with sanity check
Lodash.get vs Property dot notation for defaults
Lodash.get vs Property dot notation @movlan
Comments
Confirm delete:
Do you really want to delete benchmark?