Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining and Nullish Coalescing vs lodash get
(version: 0)
Comparing performance of:
Optional Chaining and Nullish Coalescing vs Lodash
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Optional Chaining and Nullish Coalescing
var c = obj.a?.b?.c?.d ?? 2 var d = badObj.a?.b?.c?.d ?? 2
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 and Nullish Coalescing
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining and Nullish Coalescing
5144797.0 Ops/sec
Lodash
776188.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches: Optional Chaining and Nullish Coalescing, and Lodash's `get` function. * **Optional Chaining and Nullish Coalescing**: This is a JavaScript feature introduced in ECMAScript 2020 (ES12). It allows you to navigate objects using optional chaining (`?.`) and nullish coalescing (`??`). The syntax `a?.b?.c?.d ?? 2` returns the value of `d` if it exists, or falls back to `2`. * **Lodash's `get` function**: This is a utility function from the Lodash library that allows you to safely navigate objects and provide default values. **Comparison** The benchmark compares the performance of two approaches: 1. Optional Chaining and Nullish Coalescing: Using this syntax, the code `var c = obj.a?.b?.c?.d ?? 2` will execute more quickly if the object is well-structured. 2. Lodash's `get` function: This approach requires creating a separate function call to perform the navigation, which may incur additional overhead. **Pros and Cons** * **Optional Chaining and Nullish Coalescing**: + Pros: More concise syntax, can be faster if objects are well-structured. + Cons: May not work as expected with very deep or complex object structures. * **Lodash's `get` function**: + Pros: Provides more control over the navigation process, works with any JavaScript object. + Cons: Requires an additional function call, may be slower than Optional Chaining and Nullish Coalescing. **Library Used** In this benchmark, Lodash is used. Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like string manipulation, array operations, and object navigation. **Special JS Feature/Syntax** Optional Chaining and Nullish Coalescing are the special features being tested in this benchmark. If you're not familiar with them: * Optional Chaining (`?.`) allows you to navigate objects without throwing errors if a property is missing. * Nullish Coalescing (`??`) returns the first operand if it's truthy, or the second operand if it's falsy. **Other Alternatives** If you need more control over object navigation, you could use: * Bracket notation (`obj["a"].b.c.d`) * `in` operator to check if a property exists before navigating (`if ("a" in obj && "b" in obj.a && "c" in obj.a.b))` * A custom function that checks for existence and returns the value or a default. However, these approaches may be less concise and more error-prone than Optional Chaining and Nullish Coalescing.
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?