Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash has vs native js (with .?)
(version: 0)
Comparing performance of:
lodash has 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 has
_.has(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 has
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 has
5230122.0 Ops/sec
native js (.?)
17442760.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the explanation of the provided benchmark. **What is tested?** The provided benchmark tests two approaches to access an object's property: using `_.has` from the Lodash library and using a native JavaScript syntax with optional chaining (`?.`) and dot notation (`.`). **Options compared** Two options are compared: 1. **Lodash `.has`**: This method is part of the Lodash library, which provides utility functions for various tasks, including string manipulation, array manipulation, and object inspection. 2. **Native JavaScript (.?) syntax**: This syntax allows you to access an object's property while providing a fallback value if the property does not exist. **Pros and Cons** **Lodash `.has`** Pros: * Provides a simple and straightforward way to check for an object property * Returns a boolean value indicating whether the property exists Cons: * Requires including an additional library (Lodash) in your project * May be slower than native JavaScript code due to the overhead of a function call **Native JavaScript (.?) syntax** Pros: * Eliminates the need for an additional library * Can be faster than Lodash `.has` since it's just a simple property access Cons: * Requires support for optional chaining in modern browsers (Chrome 128 and above) * May not work as expected in older browsers or environments that do not support `?.` **Library** In this case, the `_.has` method is from Lodash, which provides utility functions for working with objects. **Special JS feature/syntax** The test uses optional chaining (`?.`) with dot notation to access an object property. This syntax was introduced in ECMAScript 2018 (ES2020) and allows you to access nested properties while providing a fallback value if the intermediate property does not exist. In this case, it's used as `object?.item?.subitem.id`. **Other alternatives** If using Lodash is not feasible or preferred, other alternatives for checking object properties include: * Using a simple string-based check (e.g., `"in"`) * Utilizing a library like `propcheck` or `is-property` * Implementing custom checks within your codebase Keep in mind that these alternatives may have different trade-offs in terms of performance, readability, and maintainability. If using native JavaScript syntax without optional chaining, you can use the following approach: ```javascript function hasProperty(obj, prop) { if (obj[prop] !== undefined) return true; return false; } ``` This function checks if a property exists on an object by comparing its existence to `undefined`. If the property exists, it returns `true`, otherwise it returns `false`.
Related benchmarks:
isEmpty vs. vanilla
isEqual vs JSON.stringify
Lodash.get vs native 2022
lodash get vs native js (with .?)
Comments
Confirm delete:
Do you really want to delete benchmark?