Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
optional chaining & nullish coalescing vs _.get
(version: 0)
Comparing performance of:
Optional chaining + nullish coalescing vs _.get
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = { _TEST: { some: { very_long_key_variant: 1 } } }
Tests:
Optional chaining + nullish coalescing
const variant = a?._TEST?.some?.very_long_key_variant ?? 0;
_.get
_.get(a._TEST, 'some.very_long_key_variant', 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Optional chaining + nullish coalescing
_.get
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; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional chaining + nullish coalescing
676599360.0 Ops/sec
_.get
11172039.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark compares two approaches for accessing nested properties in an object: optional chaining (`?.`) and nullish coalescing (`??`). **Script Preparation Code** The script preparation code creates an object `a` with a deeply nested property `very_long_key_variant`. The `_TEST` property is also an object, which contains the `some` property. This setup allows us to test various levels of nesting. **Html Preparation Code** There is no HTML preparation code provided, so we'll focus on the script-based aspects of the benchmark. **Individual Test Cases** There are two test cases: 1. **Optional Chaining + Nullish Coalescing (`?.` and `??`)** The benchmark definition uses optional chaining to access `very_long_key_variant`, which returns `undefined` if the property is not found. To avoid this, it uses nullish coalescing to provide a default value of 0. ```javascript const variant = a?._TEST?.some?.very_long_key_variant ?? 0; ``` **Pros:** * Concise and expressive syntax * Easy to read and maintain **Cons:** * May be slower due to the use of optional chaining and nullish coalescing, which can lead to more overhead than direct property access. 2. **_.get (Underscore.js library)** The benchmark definition uses the `_.get` function from the Underscore.js library, which provides a way to safely access nested properties without throwing errors. ```javascript _.get(a._TEST, 'some.very_long_key_variant', 0); ``` **Pros:** * Provides a safe and efficient way to access nested properties * Avoids errors caused by missing properties **Cons:** * Adds an external library dependency (Underscore.js) * May introduce additional overhead due to the function call **Library: Underscore.js** Underscore.js is a popular utility library for JavaScript that provides a set of functional programming helpers, including `_.get`. It's designed to make functional programming easier and more efficient. **Other Alternatives** If you prefer not to use optional chaining or nullish coalescing, you could use direct property access with error handling: ```javascript const variant = a._TEST.some.very_long_key_variant || 0; ``` However, this approach can be slower and less concise than the original code. Alternatively, you could use other libraries like Lodash's `get` function or even write your own implementation for accessing nested properties safely. **Benchmark Result** The latest benchmark result shows that both approaches have similar execution speeds, but with different characteristics: * **Optional Chaining + Nullish Coalescing**: Higher number of executions per second (19,363,354) due to the use of optional chaining and nullish coalescing. * **_.get**: Lower number of executions per second (3,519,918) due to the overhead introduced by the Underscore.js library. Keep in mind that these results may vary depending on the specific environment and browser used.
Related benchmarks:
Optional Chaining versus _.get lodash
Optional Chaining versus _.get Lodash
Optional Chaining versus _.get lodash (with obj in the optional chain test)
Optional Chaining versus _.get lodash aaaaa
Optional Chaining versus _.get lodash without badObj
Comments
Confirm delete:
Do you really want to delete benchmark?