Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional chaining vs lodash get2
(version: 0)
Some
Comparing performance of:
optional vs Lodash
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
obj?.a?.b?.c?.d || 2
Lodash
_.get(obj, "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
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):
I'll break down the benchmark for you. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: optional chaining in JavaScript and the `_.get()` function from the Lodash library. **Optional Chaining vs. Lodash `_.get()`: What's Being Tested?** In the benchmark, we're comparing how fast each approach can evaluate a nested property path on an object. The first test case uses optional chaining (`?.`) to access `obj.d`, which returns `undefined` if any of the intermediate properties (`a`, `b`, or `c`) are missing. The second test case uses Lodash's `_.get()` function, which also accesses `obj.d`. **Options Compared** There are two options being compared: 1. **Optional Chaining (`?.`)**: This is a built-in JavaScript feature introduced in ECMAScript 2020. It allows you to safely navigate nested properties on an object without throwing errors if any intermediate property is missing. 2. **Lodash `_.get()` Function**: This function takes two arguments: the first is the object, and the second is a string representing the path to access. **Pros and Cons of Each Approach** **Optional Chaining (`?.`)** Pros: * Fast and lightweight * Built-in JavaScript feature * No additional dependencies or libraries required * Expressive syntax for accessing nested properties Cons: * Only works in modern browsers that support ECMAScript 2020 (Chrome 87+) * May not be supported by older browsers or environments **Lodash `_.get()` Function** Pros: * More mature and widely supported library * Works with a broader range of browsers and environments * Can handle more complex path expressions Cons: * Additional dependency required (Lodash library) * Less expressive syntax than optional chaining * Slower performance due to the extra indirection (function call) **Library Used: Lodash** The `_.get()` function is part of the Lodash library, which provides a set of utility functions for JavaScript development. The library is widely used and supported in modern web development. **Special JS Feature/Syntax: Optional Chaining (`?.`)** Optional chaining was introduced as a built-in feature in ECMAScript 2020. It allows you to safely navigate nested properties on an object without throwing errors if any intermediate property is missing. This feature is supported by most modern browsers, including Chrome 87 and later. **Alternative Approaches** If you don't have access to Lodash or prefer not to use it, you can also implement the `_.get()` function yourself using a recursive approach: ```javascript function _get(obj, path) { const parts = path.split('.'); for (const part of parts) { if (!obj || typeof obj[part] !== 'object') return null; obj = obj[part]; } return obj; } ``` Alternatively, you can use a simple loop to access the nested properties: ```javascript function _get(obj, path) { const parts = path.split('.'); let result = obj; for (const part of parts) { if (!result || typeof result[part] !== 'object') return null; result = result[part]; } return result; } ``` These approaches require more manual implementation and may not be as efficient as using Lodash's `_.get()` function or optional chaining.
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
Manual optional Chaining versus _.get lodash versus ? optional chaining
Optional Chaining versus _.get lodash without badObj
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?