Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash hasIn vs get
(version: 1)
Comparing performance of:
lodash hasIn vs lodash get
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script scr="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Script Preparation code:
var testObject = { a: { b: 1, }, }
Tests:
lodash hasIn
_.hasIn(testObject, 'a.b')
lodash get
_.get(testObject, 'a.b')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash hasIn
lodash get
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash hasIn
4947904.5 Ops/sec
lodash get
5493636.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The given benchmark compares two approaches for accessing properties in deeply nested JavaScript objects using the lodash library: `_.hasIn` and `_.get`. ### Benchmark Overview - **Library Used**: Lodash - **Purpose**: Lodash is a popular JavaScript utility library that provides modular functions for manipulating arrays, objects, and more. It simplifies common JavaScript tasks and adds utility functions that aid in working with data structures. ### Test Cases Explained 1. **`_.hasIn(testObject, 'a.b')`** - **Function**: This method checks if the path (`'a.b'`) exists in the `testObject`. - **Behavior**: If any part of the path exists, even if that part is `undefined`, it will return `true`. This means that it checks for the existence of the property along the specified path throughout the entire object structure, not just the final value. - **Use case**: `_.hasIn` is useful when you need to check for the existence of a property, regardless of its value. 2. **`_.get(testObject, 'a.b')`** - **Function**: This method retrieves the value at the specified path (`'a.b'`) if it exists; otherwise, it returns `undefined`. - **Behavior**: If the path does not exist, it will not throw an error. Instead, it gracefully returns `undefined`. - **Use case**: `_.get` is beneficial when you’re interested in directly accessing the value of a property, allowing for safe retrieval without causing errors if the property doesn’t exist. ### Performance Comparison Results - The benchmark results show: - **`lodash get`** executes approximately **5,493,636 times per second**. - **`lodash hasIn`** executes approximately **4,947,904 times per second**. - This indicates that `_.get` is faster than `_.hasIn` in this particular test setup, likely because checking for an existing path of a property (which may involve more logical checks) is less performant than simply retrieving the value. ### Pros and Cons of Approaches - **`_.hasIn` Pros**: - Useful when the existence of a property is important, irrespective of its value. - Handles inheritance from parent objects (i.e., it checks both the current object and the prototype chain). - **`_.hasIn` Cons**: - Slightly more overhead in terms of performance compared to `_.get` since it needs to evaluate the existence of the path rather than just retrieving a value. - **`_.get` Pros**: - Very high performance for direct value retrieval. - Safe access pattern that avoids errors when the property does not exist. - Allows specifying a default value that will be returned instead of `undefined`. - **`_.get` Cons**: - Does not provide information about whether the property exists, only retrieves its value (or `undefined`). ### Other Considerations - **Native JavaScript Alternatives**: - ES6 introduced optional chaining (`obj?.a?.b`), which can safely access deeply nested properties and return `undefined` if any part of the path is `undefined`. This can be a modern and native substitute for `_.get`. - The `in` operator can be used to check for property existence in an object but does not provide a way to follow a deep path like `_.hasIn`. In conclusion, the benchmark conducted between `_.hasIn` and `_.get` reveals key performance characteristics and trade-offs between access methods for object properties in JavaScript using lodash. Depending on the specific needs—whether for performance, safety, or checking for existence—developers might choose one over the other or consider native alternatives for improved efficiency and simplicity.
Related benchmarks:
Lodash.get vs Property dot notation with sanity check pass & fail
lodash isEmpty vs ES6
Optional Chaining vs lodash _.get
Optional chaining vs lodash get2
?. versus _.get lodash
Object Property Test
Lodash has vs Typescript in
Lodash IsEmpty vs Object.keys().length for checking if object has content
lodash pick vs omit v2
Comments
Confirm delete:
Do you really want to delete benchmark?