Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native optional chaining vs lodash _.get
(version: 0)
Comparing performance of:
?. vs _.get
Created:
3 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 object = { name: "a", address: { street: { name: "streetname", number: 5 }, country: { name: "germany", countrycode: "DE" } } }; var objectWithUndefined = { name: "a", address: undefined, country: { name: "germany", countrycode: "DE" } };
Tests:
?.
// Native const x = object?.address?.street?.number; const y = objectWithUndefined?.address?.street?.number;
_.get
const x = _.get(object, "address.street.number"); const y = _.get(objectWithUndefined, "address.street.number");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
?.
_.get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
?.
140985488.0 Ops/sec
_.get
4981718.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches to measure performance. The benchmark in question tests two methods: native optional chaining (`?.`) and Lodash's `_.get()` function. **Options Compared** The benchmark compares the performance of: 1. **Native Optional Chaining (`?.`)**: This is a built-in feature in JavaScript that allows you to safely navigate through nested object properties, providing a safe way to avoid null pointer exceptions. 2. **Lodash's _.get() function**: Lodash is a popular JavaScript library that provides a range of utility functions, including `_.get()` which performs similar functionality to optional chaining. **Pros and Cons** ### Native Optional Chaining (`?.`) Pros: * Lightweight and efficient, as it only checks for null or undefined values. * Built-in feature in JavaScript, so no additional dependencies are required. Cons: * May not work across all browsers, as its implementation can vary. In this benchmark, both Firefox 111 and the test machine's browser seem to support it. ### Lodash's _.get() function Pros: * More robust than native optional chaining, as it allows for more complex path specifications. * Widely supported across different JavaScript environments, including older browsers that may not have the built-in feature. Cons: * Adds an additional dependency (Lodash) which can increase page load times. * May be slightly heavier due to the overhead of function calls and string manipulation. **Library: Lodash** Lodash is a popular JavaScript library developed by Isaac Zeldman. It provides a range of utility functions, including `_.get()` that allows you to safely navigate through nested object properties. In this benchmark, Lodash's `_.get()` function is used to perform the same operation as native optional chaining (`?.`). **Special JS Feature: None** There are no special JavaScript features or syntaxes being tested in this benchmark. Both approaches rely on standard JavaScript language constructs. **Alternative Approaches** Other alternatives to compare performance could include: * Using a different library or implementation, such as the `dot()` function from jQuery or the `accessorize` function from the `accord.js` library. * Implementing custom logic using regular expressions or other string manipulation techniques. * Using different data structures, such as arrays instead of objects. It's worth noting that the choice of approach often depends on specific use cases and requirements. For example, if you need to perform complex path specifications, Lodash's `_.get()` function may be a better choice. However, for simple use cases where performance is critical, native optional chaining (`?.`) might be sufficient. Keep in mind that benchmarking JavaScript code can be complex and dependent on various factors, such as browser versions, environment, and hardware. The results of this benchmark should be taken as a general indication of the relative performance of these two approaches rather than absolute values.
Related benchmarks:
Optional Chaining versus _.get lodash (with obj in the optional chain test)
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
Comments
Confirm delete:
Do you really want to delete benchmark?