Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining vs Optional Chaining null safe vs _.get lodash
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash vs Optional Chaining v2
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Optional Chaining
(obj.a == null ? undefined : obj.a.b == null ? undefined : obj.a.b.c == null ? undefined : obj.a.b.c.d) || 2 (badObj.a == null ? undefined : badObj.a.b == null ? undefined : badObj.a.b.c == null ? undefined : badObj.a.b.c.d) || 2
Lodash
_.get(obj, "a.b.c.d", 2) _.get(badObj, "a.b.c.d", 2)
Optional Chaining v2
obj?.a?.b?.c?.d || 2; badObj?.a?.b?.c?.d || 2;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Optional Chaining
Lodash
Optional Chaining v2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its results. **Benchmark Definition** The benchmark tests three approaches to access nested properties in an object: 1. **Optional Chaining**: This is a syntax introduced in ECMAScript 2020 that allows using `?.` to safely navigate through nested objects, returning `undefined` if any step fails. 2. **Lodash's _.get() method**: Lodash is a popular utility library for JavaScript. Its `.get()` method provides a safe way to access nested properties in an object, returning `undefined` if any step fails. The benchmark also includes a comparison with the older syntax: 3. **Older Syntax (with conditional checks)**: This approach uses explicit conditional checks (`obj.a == null ? undefined : obj.a.b == null ? undefined : ...`) to navigate through nested objects. **Options Compared** * Optional Chaining vs Older Syntax * Lodash's _.get() method vs Older Syntax **Pros and Cons of Each Approach** 1. **Optional Chaining**: * Pros: concise, readable, and safer (since it automatically stops navigation if any step fails). * Cons: may be slower than explicit checks, since JavaScript has to parse the syntax. 2. **Lodash's _.get() method**: * Pros: provides a consistent and reliable way to access nested properties, with good performance. * Cons: adds an external dependency (the Lodash library), which may not be desirable for all projects. 3. **Older Syntax**: * Pros: widely supported by older browsers and without additional dependencies. * Cons: less readable and more prone to errors due to the explicit checks. **Library Used** Lodash is a popular utility library that provides various helper functions, including `.get()`, for working with objects in JavaScript. In this benchmark, Lodash's `.get()` method is used to access nested properties in an object. **Special JS Feature or Syntax** Optional Chaining (`.?.`) is a new syntax introduced in ECMAScript 2020. It allows using `?.` to safely navigate through nested objects, returning `undefined` if any step fails. **Other Alternatives** * If you prefer not to use Optional Chaining, you can use the older syntax with explicit conditional checks. * Alternatively, you can use other libraries like Lo-Dash or Underscore.js, which provide similar functionality to Lodash. In summary, the benchmark tests three approaches to access nested properties in an object: Optional Chaining, Lodash's _.get() method, and the older syntax. The results show that Optional Chaining is the fastest approach, followed closely by Lodash's _.get() method, while the older syntax is slower due to its explicit checks.
Related benchmarks:
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
Manual optional Chaining versus _.get lodash versus ? optional chaining
Optional Chaining versus _.get lodash without badObj
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?