window.obj = { a: 2 }
Object.prototype.hasOwnProperty.call(obj, 'a');
obj.hasOwnProperty('a');
obj.hasOwnProperty ? obj.hasOwnProperty('a') : true;
typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty('a') : true;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.prototype.hasOwnProperty | |
obj.hasOwnProperty | |
null check | |
function check |
Test name | Executions per second |
---|---|
Object.prototype.hasOwnProperty | 3080517.0 Ops/sec |
obj.hasOwnProperty | 6703662.5 Ops/sec |
null check | 3303243.5 Ops/sec |
function check | 3488964.5 Ops/sec |
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:
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.obj.hasOwnProperty('a')
: This approach directly calls the hasOwnProperty
method on the obj
object, passing the property name as an argument.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.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:
Object.prototype.hasOwnProperty.call(obj, 'a')
:obj.hasOwnProperty('a')
:hasOwnProperty
method on the object.hasOwnProperty
exists in the object's prototype chain.obj.hasOwnProperty ? obj.hasOwnProperty('a') : true
:typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty('a') : true
: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.