Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES2020 Optional Chaining versus _.get lodash
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Optional Chaining
obj?.a?.b?.c?.d || 2 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
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 142 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining
104059464.0 Ops/sec
Lodash
4240322.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The current benchmark measures the performance of two approaches: ES2020 Optional Chaining (`?.`) versus Lodash's `_.get` method. The goal is to determine which approach is faster for accessing nested properties in objects. **ES2020 Optional Chaining** ES2020 introduced a new operator called optional chaining (`?.`). It allows you to access nested properties in an object without throwing a `NaN` or returning `undefined` if any of the intermediate steps are `null` or `undefined`. The syntax is as follows: ```javascript obj?.a?.b?.c?.d // returns d if obj is not null or undefined, otherwise returns NaN or 0 (default) ``` **Lodash's `_get` method** Lodash's `_get` method is a utility function that allows you to access nested properties in objects. The syntax is as follows: ```javascript _.get(obj, "a.b.c.d", 2) // returns d if obj is not null or undefined, otherwise returns 2 (default) ``` **Comparison** The benchmark tests the performance of both approaches on two test cases: 1. `obj?.a?.b?.c?.d || 2` (ES2020 Optional Chaining) 2. `.get(obj, "a.b.c.d", 2)` (Lodash's `_get` method) In both test cases, if the intermediate steps are `null` or `undefined`, the function returns a default value (`2` in this case). **Pros and Cons** **ES2020 Optional Chaining:** Pros: * Concise syntax * More expressive and readable code * Built-in support in modern JavaScript engines (Chrome 121 and later) Cons: * May lead to confusing code if not used carefully * Not supported by older browsers or environments **Lodash's `_get` method:** Pros: * Well-established and widely adopted library * Provides a clear and consistent API for accessing nested properties * Supports older browsers and environments Cons: * Adds extra overhead due to the library call * May require additional imports and setup **Other Considerations** When choosing between ES2020 Optional Chaining and Lodash's `_get` method, consider the following factors: * Development speed and readability: If you need to access nested properties frequently, optional chaining may be a better choice for its concise syntax and expressiveness. * Compatibility: If you need to support older browsers or environments, Lodash's `_get` method may be a safer choice. **Alternatives** If you're not convinced about using ES2020 Optional Chaining or Lodash's `_get` method, consider the following alternatives: 1. **Use the `in` operator**: Instead of accessing nested properties with optional chaining or Lodash's `_get`, use the `in` operator to check if a property exists before attempting to access it. ```javascript if ("a" in obj && "b" in obj.a && "c" in obj.b.a) { console.log(obj.b.a.c); } ``` 2. **Use a utility library**: If you need to perform complex nested property accesses frequently, consider using a dedicated utility library like Lodash or a similar alternative. In summary, the benchmark highlights the performance difference between ES2020 Optional Chaining and Lodash's `_get` method for accessing nested properties in objects. While both approaches have their pros and cons, the choice ultimately depends on your specific use case, development speed, and compatibility requirements.
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?