var object = {},
array = [],
i, test = 1000;
for (i = 0; i < 1000; i++) {
object['something' + i] = true;
array.push('something' + i);
}
object.hasOwnProperty('something' + test)
('something' + test) in object
array.indexOf('something' + test) !== -1
object['something' + test] === true
array.includes('something' + test)
Object.prototype.hasOwnProperty.call(object, 'something' + test)
Reflect.has(object, 'something' + test)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwnProperty | |
Object in | |
Array.indexOf | |
direct | |
Array includes | |
Object.prototype.hasOwnProperty.call(this.storeIndex, index) | |
Reflect.has() |
Test name | Executions per second |
---|---|
Object.hasOwnProperty | 23723580.0 Ops/sec |
Object in | 47297124.0 Ops/sec |
Array.indexOf | 627718.5 Ops/sec |
direct | 44690308.0 Ops/sec |
Array includes | 577709.1 Ops/sec |
Object.prototype.hasOwnProperty.call(this.storeIndex, index) | 9005722.0 Ops/sec |
Reflect.has() | 41147620.0 Ops/sec |
I'll break down the benchmark and its various components.
Benchmark Overview
The benchmark measures the performance of different ways to check if an object has a certain property or if an array contains a specific value. The test case uses a fixed JavaScript code to create two objects: object
and array
, with 1000 properties/array elements.
Options Compared
Pros and Cons
hasOwnProperty()
with prototype chains).indexOf()
for very large arrays.Libraries Used
Object.prototype.hasOwnProperty.call()
: This method is used in the test case to check if object
has a property with the given name. It's called through a prototype chain, which makes it slightly slower than direct calls to hasOwnProperty()
.Special JavaScript Features/Syntax
in
, includes
, Reflect.has
): These features are used in modern browsers (Chromium-based) and require specific version support.Array.prototype.indexOf()
: This method is a standard part of the Array prototype.Other Alternatives
If you need alternative approaches to performance testing, consider:
NativeMessage
API) that might improve performance for your use case.