Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining plus Nullish Coalescing versus _.get lodash
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Optional Chaining
var c = obj.a == null ? undefined : obj.a.b == null ? undefined : obj.a.b.c == null ? undefined : obj.a.b.c.d c == null ? 2 : c var d = badObj.a == null ? undefined : badObj.a.b == null ? undefined : badObj.a.b.c == null ? undefined : badObj.a.b.c.d d == null ? 2 : d
Lodash
_.get(obj, "a.b.c.d", 2) _.get(badObj, "a.b.c.d", 2)
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:
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
Optional Chaining
94085704.0 Ops/sec
Lodash
6352210.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's being tested in the provided benchmark. **Overview** The benchmark compares the performance of two approaches to access nested object properties: Optional Chaining (also known as nullish coalescing) and Lodash's `_.get()` function. **Optional Chaining** This approach uses a chained comparison syntax (`?.`) to safely navigate through the object hierarchy. The basic idea is to check if an object property exists before attempting to access it. If the property doesn't exist, the expression returns `undefined`. To avoid this issue, Optional Chaining provides two additional operators: * `?.` (optional chaining operator): Returns `undefined` if any part of the chain is `null` or `undefined`. * `??` (nullish coalescing operator): Returns one value if the other is nullish (either `null` or `undefined`). In the benchmark, Optional Chaining is used to access nested object properties using the following syntax: ```javascript var c = obj.a == null ? undefined : obj.a.b == null ? undefined : obj.a.b.c == null ? undefined : obj.a.b.c.d ``` This expression checks each level of nesting before attempting to access the property, avoiding potential errors. **Lodash's `_.get()` function** The Lodash library provides a utility function called `_.get()`, which allows you to safely navigate through object hierarchies. The basic syntax is: ```javascript _.get(obj, 'a.b.c.d', 2) ``` This expression attempts to access the property at the specified path (`'a.b.c.d'`) on the provided object (`obj`). If any part of the path doesn't exist, it returns the default value (`2`). **Pros and Cons** **Optional Chaining:** Pros: * Lightweight and efficient, as it only requires a single expression evaluation. * Eliminates the need for explicit error handling. Cons: * Can be less readable than other approaches, especially for complex nested structures. * May not perform well in extremely deep or complex object hierarchies. **Lodash's `_.get()` function:** Pros: * Provides a clear and concise syntax for navigating through object hierarchies. * Allows for easy customization of error handling behavior (e.g., default values). Cons: * Requires an additional library import, which may add overhead. * Can be less efficient than Optional Chaining due to the extra evaluation step. **Other Considerations** * For extremely deep or complex object hierarchies, Lodash's `_.get()` function might be more suitable due to its explicit error handling capabilities. * When readability is crucial, Optional Chaining's chained comparison syntax can be a better choice. **Library and Syntax Explanation** The Lodash library provides the `_.get()` function, which allows you to safely navigate through object hierarchies. This function takes three arguments: 1. The object (`obj`) to access. 2. The path (`'a.b.c.d'`) to follow. 3. The default value (optional) to return if any part of the path doesn't exist. The `_.get()` function returns either the actual property value or the default value, depending on whether the path exists. There is no special JavaScript feature or syntax being used in this benchmark aside from Optional Chaining and Lodash's `_.get()` function.
Related benchmarks:
Optional Chaining versus _.get lodash
Optional Chaining versus _.get lodash (with obj in the optional chain 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?