window.obj = { a: 2 }
Object.prototype.hasOwnProperty.call(obj, 'a');
obj.hasOwnProperty('a');
obj.hasOwnProperty && obj.hasOwnProperty('a');
typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty('a');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.prototype.hasOwnProperty | |
obj.hasOwnProperty | |
null check | |
null check function |
Test name | Executions per second |
---|---|
Object.prototype.hasOwnProperty | 7171232.5 Ops/sec |
obj.hasOwnProperty | 14153942.0 Ops/sec |
null check | 7326758.0 Ops/sec |
null check function | 7357636.5 Ops/sec |
Let's dive into the benchmark.
What is being tested?
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test case measures the performance of different approaches to check if an object has a specific property.
The benchmark tests four different methods:
Object.prototype.hasOwnProperty.call(obj, 'a')
obj.hasOwnProperty('a')
obj.hasOwnProperty && obj.hasOwnProperty('a')
typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty('a')
Options being compared:
These options are compared to determine which approach is the fastest.
Object.prototype.hasOwnProperty.call(obj, 'a')
: This method uses the hasOwnProperty
method on the object's prototype chain and checks if the specified property exists.obj.hasOwnProperty('a')
: This method directly calls the hasOwnProperty
method on the object itself.obj.hasOwnProperty && obj.hasOwnProperty('a')
: This approach uses a null check ( obj.hasOwnProperty
) followed by another call to hasOwnProperty
to ensure the property exists.typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty('a')
: This method checks if hasOwnProperty
is a function before calling it.Pros and Cons of each approach:
Object.prototype.hasOwnProperty.call(obj, 'a')
:obj.hasOwnProperty('a')
:obj
is null or undefined, which could lead to performance issues in some cases.obj.hasOwnProperty && obj.hasOwnProperty('a')
:hasOwnProperty
.typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty('a')
:hasOwnProperty
is a function, which can help avoid NPEs.Library and syntax considerations:
The test case uses the window.obj
object, which suggests that this benchmark was written for a web environment where the global scope (window
) is available. The use of typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty('a')
implies that the test author wanted to ensure that hasOwnProperty
is called as a function.
Special JS features and syntax:
There are no special JavaScript features or syntax mentioned in this benchmark definition.
Other alternatives:
Some alternative approaches that were not tested in this benchmark include:
in
operator instead of hasOwnProperty
instanceof
to check if the object is a custom instanceObject.keys()
and checking for the existence of the propertyhas
functionKeep in mind that these alternative approaches might have different performance characteristics, pros, and cons compared to the methods tested in this benchmark.