Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs exists check
(version: 0)
Comparing performance of:
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs null check vs function check
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.obj = { a: 2 }
Tests:
Object.prototype.hasOwnProperty
Object.prototype.hasOwnProperty.call(obj, 'a');
obj.hasOwnProperty
obj.hasOwnProperty('a');
null check
obj.hasOwnProperty ? obj.hasOwnProperty('a') : true;
function check
typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty('a') : true;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.prototype.hasOwnProperty
obj.hasOwnProperty
null check
function check
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of three different approaches to check if a property exists in an object. **Approaches being compared:** 1. **`Object.prototype.hasOwnProperty.call(obj, 'a')`**: This approach uses the `hasOwnProperty` method on the `Object.prototype` to check if the object has a property named `'a'`. The `call` method is used to pass the object and the property name as arguments. 2. **`obj.hasOwnProperty('a')`**: This approach directly calls the `hasOwnProperty` method on the `obj` object, passing the property name as an argument. 3. **`obj.hasOwnProperty ? obj.hasOwnProperty('a') : true`**: This approach uses a conditional expression to check if the `hasOwnProperty` property exists in the object. If it does, the expression evaluates to the result of calling `hasOwnProperty` on the object with the property name. 4. **`typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty('a') : true`**: This approach checks the type of the `hasOwnProperty` property using `typeof`. If it's a function (which it should be, since it's a method), the expression evaluates to the result of calling `hasOwnProperty` on the object with the property name. **Pros and Cons:** 1. **`Object.prototype.hasOwnProperty.call(obj, 'a')`**: * Pros: This approach is more explicit and readable, as it clearly separates the property existence check from the actual property lookup. * Cons: It's slightly slower due to the method call overhead. 2. **`obj.hasOwnProperty('a')`**: * Pros: This approach is faster since it directly calls the `hasOwnProperty` method on the object. * Cons: It may be less readable and more prone to errors if not used carefully, as it relies on the fact that `hasOwnProperty` exists in the object's prototype chain. 3. **`obj.hasOwnProperty ? obj.hasOwnProperty('a') : true`**: * Pros: This approach is concise and easy to read, but it may be slower due to the conditional expression evaluation overhead. * Cons: It's less explicit about the property existence check, making it slightly harder to understand for some readers. 4. **`typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty('a') : true`**: * Pros: This approach is fast since it uses a simple type check instead of a method call or conditional expression. * Cons: It may be less readable and more error-prone, as the type check can fail if `hasOwnProperty` is not defined in the object's prototype chain. **Library used:** None, this benchmark does not use any external libraries. **Special JavaScript features or syntax:** The test cases do not involve any special JavaScript features or syntax beyond the basic property existence checks.
Related benchmarks:
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs exists check vs 2
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs Object.hasOwn
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?