Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
?. versus _.get lodash
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
5 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:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining
252547888.0 Ops/sec
Lodash
10690506.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark measures two approaches to access nested properties in JavaScript objects: optional chaining (`?.`) and using a library like Lodash (`_.get()`). **Optional Chaining (?.)** * The `?.` syntax was introduced in ECMAScript 2020 as a part of the Object proposal. * It allows you to safely navigate through an object's property chain, avoiding null pointer exceptions. * If any step in the chain is null or undefined, the expression will return undefined instead of throwing an error. Pros: 1. Concise and expressive syntax for accessing nested properties. 2. Reduces the chance of null pointer exceptions, making code more reliable. 3. Can be used with existing function calls (e.g., `myObject?.someMethod()`). Cons: 1. May have performance implications due to the additional checks performed by the interpreter. 2. Not all browsers support this syntax yet. **Lodash (.get())** * Lodash is a popular JavaScript utility library that provides various functions for working with data structures, including object traversal. * The `_.get()` function allows you to safely access nested properties in an object, similar to optional chaining. * It also returns undefined if any step in the chain is null or undefined. Pros: 1. Well-established and widely supported by browsers (not dependent on ECMAScript features). 2. Provides a more explicit way of accessing nested properties, which can improve code readability. 3. Works with existing Lodash installations, making it a familiar choice for developers already using the library. Cons: 1. Adds additional overhead due to the library's functionality and potential function calls. 2. May require an extra dependency in your project. **Library (Lodash) Purpose** The purpose of Lodash is to provide a collection of reusable functions that make working with data structures, such as arrays, objects, and events, easier and more efficient. **Browser-Specific Considerations** For the provided benchmark result, it's worth noting that Chrome 90 appears twice with different `ExecutionsPerSecond` values. This might indicate: 1. Different test environments or configurations (e.g., different sandboxing modes). 2. Browser-specific optimizations or cache effects. 3. Potential issues with the benchmark implementation. **Alternatives** Other approaches to accessing nested properties in JavaScript objects include: 1. Using a simple function call with `in` operator (`obj['a.b.c.d']`) and checking for existence using `typeof obj['a.b.c.d'] === 'object'`. 2. Utilizing libraries like jQuery (not directly relevant here) or other data manipulation libraries. 3. Implementing your own custom function for accessing nested properties. However, the optional chaining syntax (`?.`) and Lodash's `_get()` function are more concise and expressive ways to access nested properties, making them popular choices among developers. In conclusion, when choosing between using optional chaining (`?.`) or a library like Lodash (`_.get()`), consider factors such as performance, code readability, and the specific requirements of your project.
Related benchmarks:
Optional Chaining versus _.get lodash
ES6 Optional Chaining vs TS Optional Chaining result in javascript vs vs. Lodash _.get
Optional chaining vs lodash get3
Optional Chaining versus _.get lodash without badObj
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?