var object = {},
array = [],
i, test = 500;
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 | 1061415.1 Ops/sec |
Object in | 975346.4 Ops/sec |
Array.indexOf | 16290.5 Ops/sec |
direct | 1785414.6 Ops/sec |
Array includes | 18477.4 Ops/sec |
Let's break down what's being tested on the provided JSON.
Benchmark Overview
The benchmark measures the performance of different ways to check if an object has a property or if a value exists in an array. The test cases compare:
Object.hasOwnProperty
Object in
(a non-standard operator)Array.indexOf
with a negative result indicating absenceobject['something' + test] === true
)Array.includes
Options Compared
The benchmark compares the performance of each option on different browsers, devices, and operating systems.
Pros and Cons of Each Approach
Array.indexOf
for certain edge cases.Library and Syntax Used
The benchmark uses the following libraries and syntax:
Object in
) which is only available in ECMAScript 2020+.Special JS Feature or Syntax
The benchmark uses a non-standard operator (Object in
), which was introduced in ECMAScript 2019. This makes the benchmark more modern and potentially faster, but it may not work well with older browsers.
Other Alternatives
If you're looking for alternative approaches to this benchmark, consider:
in
or hasOwnProperty
instead of Object.hasOwnProperty
Array.prototype.includes()
or Array.prototype.indexOf()
instead of Array.indexOf()
Keep in mind that the performance differences between these approaches may vary depending on the specific use case and browser environment.