Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash get vs. native optional chaining
(version: 0)
Comparing performance of:
_,get lodash vs native, some protection
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>
Tests:
_,get lodash
const props = { stops: [ { location: { address: '123 Main Street', }, }, ], }; for (let i = 0; i < 50; i++) { const address = _.get(props, 'address'); }
native, some protection
const props = { stops: [ { location: { address: '123 Main Street', }, }, ], }; for (let i = 0; i < 50; i++) { const address = props?.stops[0]?.location?.address; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_,get lodash
native, some protection
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 dive into the details of this benchmark. **Benchmark Overview** The test measures the performance difference between using Lodash's `_.get` method and native optional chaining (`?.`) in JavaScript. The goal is to determine which approach is faster for accessing nested properties in an object. **Options Compared** There are two approaches being compared: 1. **Lodash `.get` method**: This method takes a source object, a key path (a string representing the nested property paths), and returns the value at that path. 2. **Native optional chaining (`?.`)**: This is a new syntax introduced in ECMAScript 2020 (ES12) that allows chaining methods on objects without explicitly checking if an object has a property. **Pros and Cons of Each Approach** ### Lodash `.get` method Pros: * More readable and maintainable code, especially for complex nested paths * Provides better error handling and safety guarantees Cons: * Generally slower than native optional chaining due to the overhead of function call and argument checking * Requires an external library (Lodash) to be included in the project ### Native Optional Chaining (`?.`) Pros: * Faster execution speed compared to Lodash `.get` * More concise and easier to read code * Built-in to modern JavaScript engines, eliminating the need for an external library Cons: * May lead to more complex and harder-to-debug code due to its concise syntax * Requires support for ES12 syntax (not all browsers or environments support it) **Library Used** In this benchmark, Lodash is used as a reference implementation of the `.get` method. The `lodash.min.js` file is included in the benchmark using a script tag. **Special JS Feature or Syntax** The test uses native optional chaining (`?.`) syntax, which was introduced in ECMAScript 2020 (ES12). This syntax allows chaining methods on objects without explicitly checking if an object has a property. The `?.` operator returns `undefined` if the left-hand operand is null or undefined. **Alternatives** If you're interested in exploring alternative approaches to this benchmark, here are a few options: 1. **Vanilla JavaScript**: You can implement a custom `.get` method using vanilla JavaScript, without relying on Lodash. 2. **Other libraries**: There are other JavaScript libraries that provide similar functionality to Lodash, such as Ramda or js-obj-cam. 3. **ES6-only approaches**: If you're targeting modern browsers and environments that support ES12 syntax, you can use native optional chaining (`?.`) as an alternative to the Lodash `.get` method. Keep in mind that these alternatives may have different performance characteristics and trade-offs in terms of code readability and maintainability.
Related benchmarks:
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
lodash noop vs new function vs optional chaining
Comments
Confirm delete:
Do you really want to delete benchmark?