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)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwnProperty | |
Object in | |
Array.indexOf | |
direct | |
Array includes |
Test name | Executions per second |
---|---|
Object.hasOwnProperty | 24201504.0 Ops/sec |
Object in | 21076112.0 Ops/sec |
Array.indexOf | 1399870.4 Ops/sec |
direct | 20945000.0 Ops/sec |
Array includes | 1353823.5 Ops/sec |
Let's break down the benchmark and explain what is being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The benchmark compares the performance of different methods for checking if a key exists in an object:
object.hasOwnProperty('something' + test)
( 'something' + test ) in object
array.indexOf('something' + test) !== -1
object['something' + test] === true
(referred to as "direct")array.includes('something' + test)
Methods Comparison
Here's a brief description of each method:
object.hasOwnProperty('something' + test)
: This method checks if the property 'something' + test
exists in the object using the hasOwnProperty()
method. It returns a boolean value indicating whether the property is an own property (i.e., not inherited from another object).( 'something' + test ) in object
: This method uses the in
operator to check if the string 'something' + test
exists as a key in the object.array.indexOf('something' + test) !== -1
: This method searches for the string 'something' + test
in the array using the indexOf()
method and checks if it's not equal to -1
, indicating the presence of the element.object['something' + test] === true
(direct): This method directly accesses the property 'something' + test
on the object and compares its value with true
.array.includes('something' + test)
: This method uses the includes()
method to check if the string 'something' + test
is an element of the array.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
object.hasOwnProperty('something' + test')
:( 'something' + test ) in object
:array.indexOf('something' + test) !== -1
:object['something' + test] === true (direct)
:array.includes('something' + test)
:Library Usage
There is no explicit library usage mentioned in the benchmark code. However, the includes()
method on arrays uses a proprietary algorithm implemented in WebKit (Chrome's rendering engine) that may not be fully compatible with other browsers.
Special JS Features or Syntax
No special JavaScript features or syntax are used in this benchmark. The code is relatively straightforward and uses standard JavaScript constructs.
Alternatives
If you're interested in exploring alternative methods, here are a few:
object.hasOwnProperty('something' + test)
:in
operator with a proxy object to avoid inheritance issues.Symbol.has()
method (available in modern browsers).( 'something' + test ) in object
:Object.keys()
and Array.prototype.includes()
methods for faster lookup.array.indexOf('something' + test) !== -1
:Array.prototype.findIndex()
method (available in modern browsers).Please note that some of these alternatives may have performance implications, compatibility issues, or require additional code complexity.