Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash vs ES11 Optional Chaining
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash vs ES11 Optional Chaining
Created:
5 years ago
by:
Registered User
Jump to the latest result
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)
ES11 Optional Chaining
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
ES11 Optional Chaining
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):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing three different approaches to access nested properties in an object: 1. **Optional Chaining (ES11)**: This approach uses the nullish coalescing operator (`??`) to safely navigate through the object structure, returning `undefined` if any step of the navigation is null or undefined. 2. **Lodash's _.get() method**: Lodash provides a utility function called `_get()` that allows you to access nested properties in an object while handling null or undefined values. 3. **Traditional dot notation with null checks**: This approach uses explicit null checks using `== null` comparisons to navigate through the object structure, which can lead to more code and potential errors. **Options Compared** The three approaches are compared in terms of: * Performance: How fast each approach is to execute * Code complexity: The amount of code required to implement each approach * Error handling: How well each approach handles null or undefined values **Pros and Cons of Each Approach** 1. **Optional Chaining (ES11)**: * Pros: Simple, readable, and efficient. It's a built-in feature in modern JavaScript engines. * Cons: May not be supported by older browsers or JavaScript engines. 2. **Lodash's _.get() method**: * Pros: Widely used and well-maintained library, providing a safe and robust way to access nested properties. * Cons: Adds an external dependency, which may increase bundle size and complexity. 3. **Traditional dot notation with null checks**: * Pros: Easy to understand and implement for those familiar with traditional JavaScript. * Cons: More verbose, prone to errors due to manual null checks, and can lead to performance issues. **Library Used** The benchmark uses Lodash as the utility library for its `_get()` method. Lodash is a popular JavaScript library that provides a set of reusable functions for tasks like array manipulation, string manipulation, and object manipulation. **Special JS Feature/Syntax** None mentioned in this specific benchmark. However, Optional Chaining (ES11) is a relatively new feature introduced in ECMAScript 2020, which allows for more concise and expressive way to access nested properties. **Alternatives** Other alternatives to the above approaches include: * **Braced notation**: Using braced notation (`obj.a.b.c.d`) instead of dot notation. * **Prototype chain lookup**: Using `obj_a_b_c_d` syntax to access nested properties using prototype chain lookup. * **Template literals**: Using template literals (`${obj.a.b.c.d}`) to access nested properties, although this is more suited for string interpolation than property access. Keep in mind that the choice of approach depends on the specific use case, personal preference, and the trade-offs between performance, code complexity, and error handling.
Related benchmarks:
Optional Chaining versus _.get lodash (with obj in the optional chain test)
ES6 Optional Chaining vs. ES6 Optional Chaining vs. Lodash _.get
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?