Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Manual optional Chaining versus _.get lodash versus ? optional chaining
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash vs actual optional chaining
Created:
5 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)
actual 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
actual 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):
**What is tested on the provided JSON?** The provided JSON represents a JavaScript benchmark test case that compares the performance of three approaches: 1. **Manual optional Chaining**: This approach uses explicit null checks to navigate through nested objects using the `==` operator. 2. **Lodash's _.get() function**: This is a utility function from the Lodash library that provides a safer and more concise way to access nested object properties while handling null values. 3. **Actual optional chaining**: This approach uses the new optional chaining feature introduced in ECMAScript 2020, which allows for more expressive and safe navigation through nested objects using the `?.` operator. **Options compared:** The benchmark compares the performance of each approach on a specific test case: * The first two approaches use an identical syntax to access a deeply nested object property (`obj.a.b.c.d`). * The actual optional chaining approach uses the new syntax with optional chaining (`obj?.a?.b?.c?.d`) and also includes null checks using `|| 2`. **Pros and cons of each approach:** 1. **Manual optional Chaining**: * Pros: Simple, well-known syntax, easy to understand. * Cons: Inefficient due to multiple null checks, can lead to errors if not done correctly. 2. **Lodash's _.get() function**: * Pros: Provides a safe and concise way to access nested object properties while handling null values. * Cons: Requires including an external library (Lodash), may have overhead due to the extra function call. 3. **Actual optional chaining**: * Pros: More expressive, safer, and efficient than manual optional chaining, as it avoids explicit null checks. * Cons: New syntax introduced in ECMAScript 2020, which may not be widely supported yet. **Library used:** The benchmark uses Lodash version 4.17.5 for its _.get() function. **Special JS feature or syntax:** The benchmark takes advantage of the new optional chaining feature introduced in ECMAScript 2020, which allows for more expressive and safe navigation through nested objects using the `?.` operator. This syntax is only available in modern browsers that support ECMAScript 2020. **Other considerations:** * The benchmark's performance results are likely influenced by factors such as browser caching, JavaScript engine optimizations, and network latency. * The actual optional chaining approach may have a slight performance advantage due to its ability to avoid explicit null checks, but the difference is likely small compared to other factors affecting performance. * To get meaningful results from this benchmark, it's essential to consider the specific use case and requirements of your application. **Other alternatives:** If you're looking for alternative approaches to achieve similar results without using optional chaining or Lodash's _.get() function, you could explore: * Using a different syntax or API for accessing nested object properties (e.g., `obj.a && obj.a.b && ...`) * Implementing your own null check and value retrieval logic * Using other libraries or frameworks that provide similar functionality (e.g., moment.js for date manipulation) However, these alternatives may introduce additional complexity, overhead, or performance trade-offs compared to the original approaches used in this benchmark.
Related benchmarks:
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 chrome vs lodash get
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?