Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash (with obj in the optional chain test)
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}}
Tests:
Optional Chaining
obj.a == null ? undefined : obj.a.b == null ? undefined : obj.a.b.c == null ? undefined : obj.a.b.c.d
Lodash
_.get(obj, "a.b.c.d")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Optional Chaining
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 26_2_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/143.0.7499.151 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 143 on iOS 26.2.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining
460435456.0 Ops/sec
Lodash
12699243.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing the performance of two approaches to access nested properties in an object: `Optional Chaining` (using the `?.` syntax) versus using the `_get` method from the Lodash library. **What's Being Tested?** The test cases are designed to measure the execution time of accessing nested properties in the following scenarios: 1. Accessing a non-existent property (`obj.a == null ? undefined : ...`) 2. Accessing an existing property (`obj.a.b == null ? undefined : ...`) The two approaches being compared are: **Option 1: Optional Chaining** Using the `?.` syntax to access nested properties, like this: ```javascript const result = obj.a?.b?.c?.d; ``` This approach checks if each property exists before attempting to access it. If any of the properties do not exist, the expression will return `undefined`. **Option 2: Lodash _.get** Using the `_get` method from the Lodash library to access nested properties, like this: ```javascript const result = _.get(obj, "a.b.c.d"); ``` This approach takes an object and a string representing the property path as arguments. It will return `undefined` if any of the properties in the path do not exist. **Pros and Cons** **Optional Chaining:** Pros: * More concise and readable code * Reduces errors caused by null pointer exceptions Cons: * May be slower due to the extra checks * Only works with modern JavaScript engines that support optional chaining (not older browsers) **Lodash _.get:** Pros: * Works in all browsers, including older ones * Can be more flexible when working with complex property paths Cons: * More verbose code * Requires the Lodash library to be included **Library: Lodash** The `_get` method is part of the Lodash library, a popular utility library for JavaScript. The purpose of Lodash is to provide a set of helper functions that make common programming tasks easier. **Special JS Feature/Syntax** None are mentioned in this benchmark, so we won't delve into any special features or syntaxes. **Alternatives** Other alternatives for accessing nested properties include: * Using `in` operator and conditional statements * Using a loop to access each property in the path * Using a recursive function to traverse the object However, these approaches may be less efficient and more error-prone than the two methods being compared. I hope this explanation helps you understand what's being tested in this benchmark!
Related benchmarks:
_.get Benchmark Test
ES6 Optional Chaining vs TS Optional Chaining result in javascript vs vs. Lodash _.get
Optional Chaining versus _.get lodash without badObj
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?