Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.hasOwn vs Object.prototype.hasOwnProperty
(version: 1)
Comparing performance of:
Object.hasOwn vs Object.prototype.hasOwnProperty
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.myObject = {}; window.mySet = new Set(); for (let i = 1; i < 10_000_000; i++) { window.myObject[i] = null; window.mySet.add(i); }
Tests:
Object.hasOwn
for (let i = 10_000_000; i > 0; i--) { Object.hasOwn(window.myObject, i); }
Object.prototype.hasOwnProperty
for (let i = 10_000_000; i > 0; i--) { window.myObject.hasOwnProperty(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.hasOwn
Object.prototype.hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.hasOwn
0.8 Ops/sec
Object.prototype.hasOwnProperty
0.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark tests the performance of two methods for checking whether an object has a specified property: `Object.hasOwn` and `Object.prototype.hasOwnProperty`. ### Options Compared 1. **`Object.hasOwn`**: - Introduced in ECMAScript 2022 (ES13), this method is a static method of the `Object` constructor, designed to determine if an object has a specified property as its own property. - The method signature is `Object.hasOwn(obj, prop)`, and it returns a boolean indicating whether the property `prop` exists directly on the object `obj`. 2. **`Object.prototype.hasOwnProperty`**: - This is a traditional method available in JavaScript objects since the language was created. It’s an instance method that can be called on any object to check if it has a property as its own. - The method signature is `object.hasOwnProperty(prop)`, where `object` is the instance in which you are checking for the property. ### Pros and Cons #### Pros of **`Object.hasOwn`**: - **Conciseness**: Being a static method, it can be called directly on `Object`, which can make the code somewhat cleaner, as it doesn’t need to reference the specific object instance. - **Consistency**: It leads to more uniform function calls when using cold, generic property access. #### Cons of **`Object.hasOwn`**: - **Browser Support**: As a relatively new addition to the JavaScript specification, older browsers may not support this method yet. #### Pros of **`Object.prototype.hasOwnProperty`**: - **Widespread Support**: It has been available since JavaScript's early days, so it works on virtually all browsers and environments, making it very reliable for legacy applications. - **Compatibility**: Works seamlessly with all objects since any object inherits from `Object.prototype`. #### Cons of **`Object.prototype.hasOwnProperty`**: - **Verbosity**: Requires a reference to the instance on which the method is called, which may result in slightly more verbose code than using `Object.hasOwn`. - **Potential for Overriding**: If an object has its own property named `hasOwnProperty`, it can shadow the inherited method, leading to unexpected results. ### Other Considerations - **Performance**: The benchmark results show that invoking `Object.prototype.hasOwnProperty` executed slightly faster than `Object.hasOwn` in this test setup. This may vary depending on the JavaScript engine and other environmental factors. - **Use Cases**: The choice between using these two methods should take into account the specific use case and environment. For newer codebases or applications targeting modern browsers, `Object.hasOwn` is a viable and clean option. Conversely, legacy applications or those needing wider compatibility may prefer the traditional method. ### Alternatives - **Using the `in` operator**: This checks if a property exists in an object or its prototype chain, but it doesn't differentiate between own and inherited properties. It would be used like `if (i in window.myObject)`. - **Reflect.has**: Another alternative introduced in ES6, which is a more functional approach but behaves similarly to `Object.hasOwn`. It can be used as `Reflect.has(window.myObject, i)`. Overall, the choice of method will depend on the specific needs of the project, the target environment, and the performance characteristics relevant to the task at hand.
Related benchmarks:
hasOwnProperty speed
Object.keys.length vs sum with for
guarded for-in vs object.keys
Set vs Object iteration
Object.keys.length vs sum
Set.prototype.has vs Object.hasOwn
Object.hasOwn Set.prototype.has Object.prototype.hasOwnProperty in
Object vs Map delete and lookup
variable vs property (var vs this.property) - v2
Comments
Confirm delete:
Do you really want to delete benchmark?