Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.get() vs Optional Chaining
(version: 0)
Comparing performance of:
Lodash _.get() vs Optional Chaining
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Lodash _.get()
_.get(obj, "a.b.c.d", 2) _.get(badObj, "a.b.c.d", 2)
Optional Chaining
obj?.a?.b?.c?.d || 2 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
Lodash _.get()
Optional Chaining
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash _.get()
12770384.0 Ops/sec
Optional Chaining
270282752.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on MeasureThat.net. **Benchmark Description** The benchmark compares two approaches for accessing nested properties in JavaScript objects: Lodash's `_get()` method and optional chaining (also known as the optional catcher syntax, introduced in ECMAScript 2020). **Options Compared** There are two main options being compared: 1. **Lodash's `_get()` method**: This is a utility function from the Lodash library that provides a way to access nested properties of an object. 2. **Optional Chaining (?.)**: This is a new syntax introduced in ECMAScript 2020, which allows you to access nested properties of an object without the need for explicit `null` checks or chaining methods. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: ### Lodash's `_get()` method Pros: * More verbose, but provides more control over error handling and property access. * Can be used with custom properties or arrays. Cons: * Requires importing the Lodash library. * May add unnecessary overhead due to function call and object lookup. ### Optional Chaining (?.) Pros: * Less verbose and easier to read. * Faster performance, as it avoids explicit `null` checks and chaining methods. * Native support in modern JavaScript engines. Cons: * Limited control over error handling and property access. * May not work correctly with custom properties or arrays. **Library: Lodash** Lodash is a popular JavaScript library that provides various utility functions for tasks like array manipulation, object iteration, and more. The `_get()` method is one of its many useful helpers for accessing nested properties of objects. **Special JS Feature/Syntax: Optional Chaining (?.)** Optional Chaining was introduced in ECMAScript 2020 as a new syntax for accessing nested properties of objects without explicit `null` checks or chaining methods. It allows you to write more concise and expressive code, such as `obj?.a?.b?.c`, which is equivalent to `obj && obj.a && obj.a.b && obj.a.b.c`. **Other Alternatives** If you're interested in exploring other approaches for accessing nested properties of objects, here are a few alternatives: 1. **Brackets (`[]`)**: You can use square brackets `[]` to access nested properties of an object, like this: `obj['a']['b']['c']`. 2. **Destructuring assignment**: You can use destructuring assignment to extract nested properties from objects, like this: `const { a, b, c } = obj;`. 3. **Built-in `in` operator**: You can use the `in` operator to check if an object has a particular property, and then access it using square brackets `[]`. Keep in mind that these alternatives may have different performance characteristics or readability trade-offs compared to Lodash's `_get()` method and optional chaining.
Related benchmarks:
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
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?