Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.prototype.has vs Object.hasOwn
(version: 0)
Comparing performance of:
Object.hasOwn vs Set.prototype.has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.myObject = {}; window.mySet = new Set(); for (let i = 1; i < 1001; i++) { window.myObject[i] = null; window.mySet.add(i); }
Tests:
Object.hasOwn
for (let i = 1000; i > 0; i--) { Object.hasOwn(window.myObject, i); }
Set.prototype.has
for (let i = 1000; i > 0; i--) { window.mySet.has(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.hasOwn
Set.prototype.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.hasOwn
11744.2 Ops/sec
Set.prototype.has
1843018.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **`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. 2. **`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:** 1. **`Set.prototype.has`**: * Pros: More efficient for large sets, as it uses a hash table under the hood, making lookups O(1) on average. * Cons: May be slower for small sets or single-element sets due to overhead of creating and managing the underlying data structure. 2. **`Object.hasOwn`**: * Pros: Widely supported across browsers and platforms, as it's a part of the Object prototype. * Cons: Less efficient than `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: * Using the `in` operator (`i in window.mySet`) to check if an element exists in the set. * Implementing a custom lookup function using an array or hash table. 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.
Related benchmarks:
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
Object.prototype.hasOwnProperty vs obj.hasOwnProperty vs exists check vs 2
Object.hasOwn Set.prototype.has Object.prototype.hasOwnProperty in
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?