var object = {},
array = [],
i, test = 50000;
for (i = 0; i < 50000; 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 | 3677822.2 Ops/sec |
Object in | 3562511.0 Ops/sec |
Array.indexOf | 3083.4 Ops/sec |
direct | 3514136.8 Ops/sec |
Array includes | 3155.7 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches. The benchmark in question tests five methods for checking if a property exists or can be found within an object, array, or array with indexOf
method.
Tested Approaches
The test compares the following approaches:
object['something' + test] === true
)includes
method to check if an element exists in an arrayOptions Compared
The test compares the performance of these five approaches on a large dataset of 50,000 objects and 50,000 elements.
Pros and Cons of Each Approach:
hasOwnProperty
, but with a slightly more modern syntax.hasOwnProperty
.indexOf !== -1
due to the overhead of regular expressions.Library Used
The test uses no external libraries beyond standard JavaScript features.
Special JS Features or Syntax
None mentioned, but it's worth noting that some browsers may optimize certain methods using WebAssembly or other advanced technologies.
Alternatives
Other approaches could be used for this benchmark, such as:
has
functionHowever, these alternatives are not tested in the provided benchmark.