Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash
(version: 0)
Comparing performance of:
Optional Chaining vs Lodash
Created:
8 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)
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:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional Chaining
146743920.0 Ops/sec
Lodash
5554770.5 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, and their pros and cons. **Benchmark Definition** The benchmark is testing two approaches to accessing nested properties of objects: 1. **Optional Chaining**: This is a JavaScript feature introduced in ECMAScript 2020 (ES12). It allows you to access nested properties of an object while providing a default value if any of the intermediate steps are null or undefined. 2. **Lodash _.get() function**: Lodash is a popular utility library for JavaScript that provides various functions for common tasks, including data manipulation and string manipulation. **Options Compared** The benchmark compares two options: 1. **Optional Chaining** 2. **Lodash _.get() function with two arguments (value and default)** **Pros and Cons** **Optional Chaining** Pros: * More concise and expressive code * Faster execution, as it avoids the overhead of calling a separate function * Part of the standard JavaScript language, making it more familiar to developers Cons: * Not all browsers support ES12, so older browsers might not execute this benchmark correctly * Can be less readable for developers who are not familiar with this syntax **Lodash _.get() function** Pros: * More readable code, as the default value is explicitly specified * Works in older browsers that don't support ES12 * Part of a well-known and widely-used library, making it easier to find resources and community support Cons: * More verbose code compared to Optional Chaining * Slower execution, due to the overhead of calling a separate function * Requires including an additional library, which can add overhead **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for common tasks, such as data manipulation, string manipulation, and functional programming utilities. The _.get() function is part of this library, which allows you to safely access nested properties of objects while providing a default value. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntaxes being used in this benchmark beyond the Optional Chaining feature introduced in ES12. **Other Alternatives** If you want to compare alternative approaches to accessing nested properties, here are a few options: 1. **Brackets**: Brackets is another dot notation style for accessing nested properties of objects. 2. **Property access with multiple dots**: Accessing nested properties using multiple dots (e.g., `obj.a.b.c.d`) instead of Optional Chaining. 3. **Using a function**: Using a separate function to retrieve the value, like this: `function getValue(obj) { return obj.a && obj.a.b && obj.a.b.c && obj.a.b.c.d; }` Keep in mind that these alternatives may have different performance characteristics and code readability trade-offs compared to Optional Chaining and Lodash _.get().
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?