Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash get vs native js (with .?)
(version: 0)
Comparing performance of:
lodash get vs native js (.?)
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Script Preparation code:
object = { item: { subitem: { id: 1 } } }
Tests:
lodash get
_.get(object, 'item.subitem.id');
native js (.?)
object?.item?.subitem?.id
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash get
native js (.?)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash get
5664484.0 Ops/sec
native js (.?)
17351244.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and its test cases. **Benchmark Definition** MeasureThat.net is testing two different approaches to accessing nested properties in an object: Lodash's `_.get()` method and the native JavaScript optional chaining operator (`?.`). **Options Compared** The two options being compared are: 1. **Lodash's `_.get()` method**: This is a utility function from the popular JavaScript library Lodash that allows you to safely navigate through nested properties in an object. 2. **Native JavaScript optional chaining operator (`?.`)**: This is a new feature introduced in ECMAScript 2020, which allows you to access nested properties without throwing an error if any of the intermediate steps are null or undefined. **Pros and Cons** **Lodash's `_.get()` method** Pros: * Provides a safe and explicit way to navigate through nested properties. * Can handle errors and exceptions elegantly. * Part of a widely used and well-maintained library. Cons: * Adds extra weight to the application due to the inclusion of the Lodash library. * May introduce additional overhead compared to the native JavaScript approach. **Native JavaScript optional chaining operator (`?.`)** Pros: * Native implementation, which means it doesn't add extra weight to the application. * Fast and efficient, as it's implemented in the browser's engine. * Becomes a standard feature in modern JavaScript, making code more readable and maintainable. Cons: * Only available in modern browsers that support ECMAScript 2020 or later. * May require additional setup or configuration for older browsers. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and more. The `_.get()` method is one of its many useful helpers for working with objects. **Test Case Explanation** The two test cases are identical in terms of the code being executed: ```javascript object?.item?.subitem?.id ``` The only difference between the two approaches is how they handle null or undefined intermediate steps. Lodash's `_.get()` method will return `undefined` if any step is null or undefined, while the native JavaScript optional chaining operator (`?.`) will simply short-circuit and return `undefined`. **Other Considerations** When choosing between these two approaches, consider the trade-offs between safety, readability, and performance. If you're working in a modern browser that supports ECMAScript 2020 or later, the native JavaScript approach is likely a good choice for its speed and brevity. However, if you need to support older browsers or prefer a more explicit and safe way of navigating through nested properties, Lodash's `_.get()` method might be a better fit. **Alternative Approaches** Other alternative approaches for accessing nested properties in JavaScript include: * Using bracket notation (`object['item.subitem.id']`) * Using the dot notation (`object.item.subitem.id`) * Using a recursive function to traverse the object * Using a library like jQuery or another utility library Each of these approaches has its own pros and cons, which are worth considering depending on your specific use case.
Related benchmarks:
isEqual vs JSON.stringify
Lodash.get vs native 2022
Lodash has vs Native Javascript
lodash has vs native js (with .?)
Comments
Confirm delete:
Do you really want to delete benchmark?