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);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwn | |
Set.prototype.has |
Test name | Executions per second |
---|---|
Object.hasOwn | 4854.9 Ops/sec |
Set.prototype.has | 4503.9 Ops/sec |
Let's dive into the JavaScript microbenchmark.
What is being tested?
The provided JSON represents two test cases, comparing the performance of Set.prototype.has
and Object.hasOwn
. Both methods are used to check if an object has a certain property. The benchmark measures which method is faster in terms of executions per second.
Options compared:
The comparison is between:
Set.prototype.has
: This method is part of the Set interface, introduced in ECMAScript 2015 (ES6). It checks if an element with the specified value exists in the set.Object.hasOwn
: This method is a part of the Object prototype, also introduced in ES6. It returns a boolean indicating whether the object has the specified property.Pros and Cons:
Set.prototype.has
:Object.hasOwn
:Set.prototype.has
, especially for large datasets.Library usage:
The benchmark uses the window.myObject
and window.mySet
objects, which are created using the provided script preparation code. These objects are used to simulate a scenario where you need to check if an object has a certain property.
Special JS feature or syntax:
There is no special JavaScript feature or syntax being tested in this benchmark.
Other alternatives:
If Object.hasOwn
was not available, other alternatives might have been explored, such as:
in
operator (i in window.mySet
) to check if an element exists in the set.However, since Object.hasOwn
is widely supported and efficient for checking property existence, it's likely that this alternative was not considered in this benchmark.