Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional Chaining versus _.get lodash
(version: 0)
Comparing performance of:
lodash vs Optional
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a: {b: {c: {d: 1}}}} var badObj = {}
Tests:
lodash
let a = _.get(obj, "a.b.c.d", 2) let b = _.get(badObj, "a.b.c.d", 2)
Optional
let a = obj.a?.b?.c?.d || 2 let b = 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
Optional
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's being tested. **Benchmark Overview** The benchmark compares two approaches for accessing nested properties in JavaScript objects: 1. **Lodash _.get()**: A library function that allows you to safely navigate through nested object properties, providing a default value if any of the intermediate steps are missing. 2. **Optional Chaining (?.)**: A modern JavaScript syntax introduced in ECMAScript 2020, which provides a safe way to access nested properties without throwing errors. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object manipulation, and more. The _.get() function is specifically designed to safely navigate through nested objects, providing a default value if any of the intermediate steps are missing. **Optional Chaining (?.)** Optional Chaining is a syntax feature introduced in ECMAScript 2020 that allows you to access nested properties in an object without throwing errors. It's implemented using the `?.` operator. For example: `obj.a?.b?.c.d` The pros of Optional Chaining are: * **Concise**: It's more concise than using a library function like _.get(). * **Efficient**: Since it's just syntactic sugar, there's no performance overhead. * **Easy to read**: The code is easier to understand and write. However, the cons are: * **Browser support**: As of my knowledge cutoff in December 2023, not all browsers support Optional Chaining. Lodash provides a polyfill for older browsers that don't support it natively. * **Learning curve**: You need to learn the syntax and its limitations. **Benchmark Results** The benchmark results show that: * The Chrome 89 browser with the latest Mac OS X version executes the "Optional" test case more frequently than the "Lodash" test case, indicating a performance advantage. * Within the "Optinal" test case, the executions per second are lower for the "Lodash" approach compared to the Optional Chaining syntax. Other alternatives include: * Using bracket notation (`obj["a"]["b"]["c"].d`) instead of optional chaining or Lodash _.get() to access nested properties. * Using a try-catch block to catch errors when accessing nested properties. * Using a library like underscore.js or moment.js, which provide similar functionality to Lodash. Keep in mind that the performance difference between these approaches may not be significant for all use cases. The choice ultimately depends on your specific requirements, code readability, and personal preference.
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?