Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional chaining vs Lodash.get
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
4 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?.b?.c?.d badObj?.a?.b?.c?.d
Lodash
_.get(obj, "a.b.c.d") _.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:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining
132621976.0 Ops/sec
Lodash
6280399.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared options, pros and cons of those approaches, and other considerations. **What is being tested?** The benchmark tests two ways to access nested properties in JavaScript objects: optional chaining (`?.`) and Lodash's `_.get()` function. The test cases are designed to compare the performance of these two approaches on a specific dataset. **Options compared:** 1. **Optional Chaining (`?.`)**: This is a new JavaScript feature introduced in ECMAScript 2020, which allows you to access nested properties without throwing an error when they don't exist. 2. **Lodash's `_.get()` function**: This is a utility function from the Lodash library that provides similar functionality to optional chaining. **Pros and cons of each approach:** 1. **Optional Chaining (`?.`)**: * Pros: + Simplifies code by eliminating the need for explicit null checks. + More concise and readable. * Cons: + Only supported in modern browsers (ECMAScript 2020+) and Node.js (14+). + May lead to slower performance due to additional parsing and compilation steps. 2. **Lodash's `_.get()` function**: * Pros: + Widely supported across different JavaScript environments, including older browsers and Node.js versions. + Provides more control over the behavior, such as allowing a default value to be returned if the property doesn't exist. * Cons: + Requires an additional import statement (Lodash library). + May lead to slightly longer code and more complex logic. **Library:** In this benchmark, Lodash is used for its `_.get()` function. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, object iteration, and more. The `_.get()` function is specifically designed to access nested properties in objects, making it a good fit for this benchmark. **Special JS feature or syntax:** Optional chaining (`?.`) is a new JavaScript feature introduced in ECMAScript 2020. It allows you to access nested properties without throwing an error when they don't exist. This feature has improved the readability and conciseness of JavaScript code, especially in scenarios where null or undefined values are common. **Other alternatives:** If you prefer not to use optional chaining (`?.`) or Lodash's `_.get()` function, other approaches include: 1. Using explicit null checks: `if (obj && obj.a && obj.a.b && obj.a.b.c) { console.log(obj.a.b.c); }` 2. Using the `in` operator: `for (var prop in obj) if (prop === 'a' && typeof obj[prop] === 'object') { if (prop === 'b' && typeof obj[prop] === 'object') { if (prop === 'c' && typeof obj[prop] === 'number') { console.log(obj[prop]); } } }` 3. Using the `hasOwnProperty()` method: `if (obj.hasOwnProperty('a') && obj.a.hasOwnProperty('b') && obj.a.b.hasOwnProperty('c')) { console.log(obj.a.b.c); }` These alternatives may be more verbose and less readable than optional chaining (`?.`) or Lodash's `_.get()` function, but they can still get the job done.
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 chrome vs lodash get
Lodash _.has vs Optional Chaining
Comments
Confirm delete:
Do you really want to delete benchmark?