var object = {},
array = [],
set = new Set();
let i;
for (i = 0; i < 1000; i++) {
object['something' + i] = true;
array.push('something' + i);
set.add('something' + i);
}
object.hasOwnProperty('something' + Math.floor(Math.random() * 1000))
('something' + Math.floor(Math.random() * 1000)) in object
array.indexOf('something' + Math.floor(Math.random() * 1000)) !== -1
object['something' + Math.floor(Math.random() * 1000)]
array.includes('something' + Math.floor(Math.random() * 1000))
set.has('something' + Math.floor(Math.random() * 1000))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwnProperty | |
Object in | |
Array.indexOf | |
direct | |
Array includes | |
Set has |
Test name | Executions per second |
---|---|
Object.hasOwnProperty | 1543042.4 Ops/sec |
Object in | 1357842.9 Ops/sec |
Array.indexOf | 225389.0 Ops/sec |
direct | 1729548.6 Ops/sec |
Array includes | 217106.8 Ops/sec |
Set has | 1822450.8 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, comparing the performance of different approaches to check if an object has a specific property or value.
Benchmark Definition
The benchmark definition is created by preparing a script that generates 1000 random objects with properties named "something" followed by a unique identifier (a number between 0 and 999). The script then uses each of six different methods to access these properties:
object.hasOwnProperty('something' + Math.floor(Math.random() * 1000))
: This method checks if the object has a property with the given key.( 'something' + Math.floor(Math.random() * 1000) ) in object
: This method uses the in
operator to check if the object has a property with the given key.array.indexOf('something' + Math.floor(Math.random() * 1000)) !== -1
: This method checks if an array contains an element that matches the given value.object['something' + Math.floor(Math.random() * 1000)]
: This method directly accesses the property using bracket notation.array.includes('something' + Math.floor(Math.random() * 1000))
: This method uses the includes
method to check if an array contains an element that matches the given value.set.has('something' + Math.floor(Math.random() * 1000))
: This method checks if a Set object has a specific element.Comparison of Methods
The comparison involves executing each method a large number of times (1 million) and measuring the execution time per second. The results show that:
Set.has
is the fastest, with an average of around 200,000 executions per second.object['something' + Math.floor(Math.random() * 1000)]
) is faster than using the in
operator or hasOwnProperty
methods, but slower than Set.has
.indexOf
or includes
is relatively slow, with an average of around 150,000 executions per second.in
operator to check if an object has a property is slower than checking directly (object.hasOwnProperty
) or using Set.has
.Pros and Cons
The pros and cons of each method are:
Set.has
or direct access.in
, but more specific and reliable. Pros: More precise control over the check, but slower than Set.has
.Other Considerations
When choosing a method, consider the following factors:
Set.has
or direct access.hasOwnProperty
or check directly (object.hasOwnProperty
).Alternatives
Other alternatives to these methods include:
Keep in mind that these alternatives may have different performance characteristics and trade-offs, depending on the specific use case.