Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash versus ?.
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash vs ?.
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 == 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)
?.
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
?.
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 is being tested. **Benchmark Overview** The benchmark compares three different approaches to handle optional values in JavaScript: 1. **Optional Chaining**: A feature introduced in ECMAScript 2020 that allows you to access nested properties of an object while handling null or undefined values. 2. **Lodash's _.get() method**: A utility function from the Lodash library that provides a safe way to access nested properties of an object, returning undefined if any property is null or undefined. 3. **?. Operator**: A new operator introduced in ECMAScript 2019 that allows you to access nested properties of an object while handling null or undefined values. **Options Compared** The benchmark compares the performance of each approach on a test case where an object `obj` and another object `badObj` are created with different structure. The test case checks how many executions per second can be achieved for each approach: * `obj.a.b.c.d` * `obj?.a?.b?.c?.d` (Optional Chaining) * `_.get(obj, "a.b.c.d", 2)` (Lodash's _.get() method) * `badObj?.a?.b?.c?.d || 2` **Pros and Cons of Each Approach** * **Optional Chaining (`obj?.a?.b?.c?.d`)**: + Pros: Easy to read, concise syntax. + Cons: Requires support for ECMAScript 2020 or later. May not be compatible with older browsers or environments. * **Lodash's _.get() method**: + Pros: Widely supported library, provides a safe way to access nested properties. + Cons: Adds an extra dependency (Lodash), may introduce performance overhead due to function call and argument processing. * **?. Operator**: + Pros: New operator introduced in ECMAScript 2019, easy to read, concise syntax. + Cons: Requires support for ECMAScript 2019 or later. May not be compatible with older browsers or environments. **Library Used** Lodash is a popular utility library that provides various functions for working with JavaScript objects, arrays, and other data structures. **Special JS Feature/ Syntax** * **Optional Chaining (`?.` operator)**: A new operator introduced in ECMAScript 2019, allows you to access nested properties of an object while handling null or undefined values. * **Lodash's _.get() method**: Uses a safe approach to access nested properties of an object, returning undefined if any property is null or undefined. **Other Alternatives** If the `?.` operator and Lodash's `_._get()` method are not available or desired, other alternatives can be used: * Using traditional null checks (e.g., `obj && obj.a && obj.a.b && obj.a.b.c && obj.a.b.c.d`) * Using a library like `get-object-by-path` which provides similar functionality to Lodash's `_._get()` method. Note that the choice of alternative approach depends on the specific use case, personal preference, and requirements of the project.
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?