window.myObject = {};
window.mySet = new Set();
for (let i = 1; i < 1001; i++) {
window.myObject[i] = null;
window.mySet.add(i);
}
for (let i = 1000; i > 0; i--) {
Object.hasOwn(window.myObject, i);
}
for (let i = 1000; i > 0; i--) {
window.mySet.has(i);
}
for (let i = 1000; i > 0; i--) {
window.myObject.hasOwnProperty(i);
}
for (let i = 1000; i > 0; i--) {
i in window.myObject;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwn | |
Set.prototype.has | |
Object.prototype.hasOwnProperty | |
in |
Test name | Executions per second |
---|---|
Object.hasOwn | 15110.4 Ops/sec |
Set.prototype.has | 15103.7 Ops/sec |
Object.prototype.hasOwnProperty | 15376.8 Ops/sec |
in | 15555.2 Ops/sec |
Let's break down the provided benchmarking test case and explain what it's testing, along with its pros and cons.
Benchmark Overview
The benchmark tests the performance of three different methods to check if an object has a specific property:
Object.prototype.hasOwnProperty
Set.prototype.has
(not applicable in this context, as we're checking for an object property)in
Additionally, it tests another method that's not relevant here: Object.hasOwn
.
Script Preparation Code
The script preparation code creates two objects and a Set:
window.myObject = {};
window.mySet = new Set();
for (let i = 1; i < 1001; i++) {
window.myObject[i] = null;
window.mySet.add(i);
}
This setup is done to ensure that each test case has a similar scope and object structure.
HTML Preparation Code
There is no HTML preparation code, which means the benchmark only runs in a headless browser context ( likely using Chrome's Headless mode).
Test Cases
The benchmark defines four test cases:
hasOwnProperty
on that property.hasOwnProperty
but may not be as secure.hasOwnProperty
. However, its implementation is less standard and might depend on the browser.Pros and Cons
Here are some general pros and cons for each test case:
hasOwnProperty
, its implementation is browser-dependent.Library/Functionality Usage
In this benchmark, we don't use any external libraries. However, the usage of the Set
object and its methods (has
) might be specific to certain browsers or versions.
Special JavaScript Features/Syntax
The benchmark doesn't explicitly use or test any special JavaScript features or syntax, such as async/await or arrow functions.
Alternatives
For this specific type of benchmarking (checking for property existence), other alternatives include:
Keep in mind that the choice of alternative will depend on your specific requirements and use case.
Conclusion
In summary, this benchmark tests the performance of different methods to check if an object has a specific property. It provides insights into the execution speed and variation across different browsers, devices, and operating systems.