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)
--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) |
Test name | Executions per second |
---|---|
Object.hasOwnProperty | 3136243.0 Ops/sec |
Object in | 2864621.0 Ops/sec |
Array.indexOf | 854761.6 Ops/sec |
direct | 2887668.8 Ops/sec |
Array includes | 860361.2 Ops/sec |
Object.prototype.hasOwnProperty.call(this.storeIndex, index) | 1823277.8 Ops/sec |
Measuring the performance of JavaScript microbenchmarks can be a complex task, and there are various approaches to test the different options. Here's a breakdown of what's being tested, the pros and cons of each approach, and other considerations:
What is being tested?
The benchmark tests six different ways to check if an object or array contains a specific value:
object.hasOwnProperty('something' + test)
: checks if the object has a property with the given key.(something' + test) in object
: uses the in
operator to check if the object contains the given value.array.indexOf('something' + test) !== -1
: searches for the value in the array using indexOf
.object['something' + test] === true
: checks if the property exists and has a value of true
.array.includes('something' + test)
: uses the includes
method to search for the value in the array.Object.prototype.hasOwnProperty.call(object, 'something' + test)
: uses a custom hasOwnProperty
call on the object.Approaches and pros/cons:
object.hasOwnProperty('something' + test)
: This approach is often considered the most straightforward way to check if an object has a property.(something' + test) in object
: Using in
operator can lead to unexpected behavior or false positives if the object's prototype chain is not properly configured.array.indexOf('something' + test) !== -1
: This approach is generally faster than in
operator but can lead to false negatives if the array is not properly sorted or if the value is not found within the bounds of the search range.object['something' + test] === true
: This approach assumes that the property exists and has a value of true
, which might not always be the case.array.includes('something' + test)
: This approach is generally faster than other methods but can lead to false negatives if the array is not properly sorted or if the value is not found within the bounds of the search range.Object.prototype.hasOwnProperty.call(object, 'something' + test)
: This approach uses a custom hasOwnProperty
call on the object's prototype chain.Other considerations:
includes
method is generally faster than other approaches but can lead to false negatives or unexpected behavior if not used carefully.Alternatives:
Other alternatives to these approaches include:
&
operator to check for property existence).Map
, WeakMap
, or Set
data structures for efficient property storage and lookup.Keep in mind that these alternatives may introduce additional complexity, dependencies, or performance overhead, so it's essential to consider the trade-offs when choosing an approach.