Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.hasOwnProperty vs in
(version: 1)
Comparing performance of:
Object.hasOwnProperty vs in
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var arr = Array(10_000).fill(undefined).map((_, index) => index + 1); var obj = Object.fromEntries(arr.map((v) => [v, v]));
Tests:
Object.hasOwnProperty
arr.map((v) => obj.hasOwnProperty(v));
in
arr.map((v) => v in obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.hasOwnProperty
in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.hasOwnProperty
10738.8 Ops/sec
in
21525.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark is designed to compare the performance of two different methods for checking if an object has a property: `Object.hasOwnProperty` and the `in` operator in JavaScript. ### Options Compared 1. **`Object.hasOwnProperty`**: - **Test Case**: `arr.map((v) => obj.hasOwnProperty(v));` - **Test Name**: "Object.hasOwnProperty" - This method checks whether the specified property is a direct property of the object, not inherited through the prototype chain. 2. **`in` Operator**: - **Test Case**: `arr.map((v) => v in obj);` - **Test Name**: "in" - This operator checks whether the property exists in the object or its prototype chain. ### Pros and Cons - **`Object.hasOwnProperty`**: - **Pros**: - Clear intention: It explicitly checks only for properties that belong to the object itself (not inherited). - Safe from false positives if there are properties in the prototype chain. - **Cons**: - Slightly slower than the `in` operator in some cases, as it directly accesses the method and checks the specified property. - **`in` Operator**: - **Pros**: - Generally faster due to its simpler underlying mechanism that checks the property existence in both the object and its prototypes. - More concise syntax. - **Cons**: - Can lead to confusion if the presence of inherited properties is not properly understood, potentially leading to incorrect assumptions about object ownership. ### Other Considerations When comparing these two methods, the context of their usage is essential. If you need to ensure that the property is a direct member of the object (i.e., not inherited), `Object.hasOwnProperty` is the appropriate choice. If performance is critical and you are okay with checking for inherited properties as well, the `in` operator might be preferred. ### Library or Special Features In this specific benchmark, no external libraries are utilized, and neither special JavaScript language features or syntax are employed beyond the standard constructs available in the language. ### Alternatives Alternatives to these two methods for property checking could include: - **Using `Reflect.has`**: This is part of the Reflect API and behaves similarly to the `in` operator but may offer improved readability in certain contexts. ```javascript Reflect.has(obj, property); ``` - **Using `Object.keys()` or `Object.entries()`**: You could check if a property exists by filtering the keys/entries of the object, although this is generally less efficient: ```javascript const keys = Object.keys(obj); const exists = keys.includes(property); ``` Overall, the benchmark serves to highlight performance implications between two fundamentally different approaches to checking property existence in JavaScript objects, each suited for different scenarios depending on requirements for clarity versus performance.
Related benchmarks:
Map vs Object (white)
Access Object, Map, Array, Set with n items
Access Object, Map, Array, Set with n items #2
has vs includes
has vs includess
includes vs set 1241 2341234 1233
Array.includes vs Set.has vas Map.has vs object[k] !== undefined (length 1 million)
Set vs Object add
Set vs Object lookup
Comments
Confirm delete:
Do you really want to delete benchmark?