Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash 2020 yq
(version: 2)
A quick performance test between lodash and native optional chaining.
Comparing performance of:
Optional Chaining vs Lodash
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
Optional Chaining
// Compiled code from TSC // obj?.a?.b?.c?.d ?? 2 // badObj?.a?.b?.c?.d ?? 2 "use strict"; var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; _e = (_d = (_c = (_b = (_a = obj) === null || _a === void 0 ? void 0 : _a.a) === null || _b === void 0 ? void 0 : _b.b) === null || _c === void 0 ? void 0 : _c.c) === null || _d === void 0 ? void 0 : _d.d, (_e !== null && _e !== void 0 ? _e : 2); _k = (_j = (_h = (_g = (_f = badObj) === null || _f === void 0 ? void 0 : _f.a) === null || _g === void 0 ? void 0 : _g.b) === null || _h === void 0 ? void 0 : _h.c) === null || _j === void 0 ? void 0 : _j.d, (_k !== null && _k !== void 0 ? _k : 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:
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):
Let's break down the benchmark and explain what is being tested, compared, and their pros and cons. **What is being tested?** The benchmark compares two approaches for accessing nested object properties: JavaScript's optional chaining (`?.`) and Lodash's `_.get()` function. Specifically, it tests the performance of these approaches on a simple test case with two objects: `obj` and `badObj`. **Script Preparation Code** The script preparation code defines two variables: * `obj`: an object with nested properties `{a: {b: {c: {d: 1}}}}`. * `badObj`: an empty object (`{}`). This setup creates a simple scenario where the optional chaining and Lodash's `_get()` function are used to access the nested property `d`. **Benchmark Definition** The benchmark definition is a JavaScript code snippet that uses both approaches: * Optional Chaining: `obj?.a?.b?.c?.d ?? 2` * Lodash's `_get()`: `_.get(obj, ['a', 'b', 'c', 'd'], 2)` Both expressions are equivalent and test the performance of each approach. **What options are compared?** The two options being compared are: 1. **JavaScript's Optional Chaining (`?.`)**: a built-in feature introduced in ECMAScript 2020 that allows safe navigation through nested objects. 2. **Lodash's `_get()` function**: a utility function from the Lodash library that provides a way to safely access nested object properties. **Pros and Cons of each approach:** 1. **JavaScript's Optional Chaining (`?.`)**: * Pros: + Built-in, native feature, which means it's highly optimized for performance. + Simple and readable syntax. * Cons: + May not be supported in older browsers or JavaScript engines. 2. **Lodash's `_get()` function**: * Pros: + Widely supported by most modern browsers and JavaScript engines. + Provides a flexible way to access nested properties using arrays or strings. * Cons: + Adds an additional dependency on the Lodash library. + May have overhead due to the extra function call. **Other considerations** * The benchmark assumes that the input objects are valid and will not throw any errors. In a real-world scenario, you may need to handle edge cases and error checking. * The `??` operator (nullish coalescing) is used to provide a default value of 2 if the expression evaluates to null or undefined. **Alternatives** If you prefer not to use optional chaining, you can also consider using other methods like: * Bracket notation (`obj['a']['b']['c']['d']`) * Property access with dot notation (`obj.a.b.c.d`) However, these approaches have different performance characteristics and may be less readable than the optional chaining or Lodash's `_get()` function.
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?