Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object key in lookup
(version: 0)
Comparing performance of:
set.has vs key in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); var b = {1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true}
Tests:
set.has
return a.has(9)
key in
return 9 in b;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set.has
key in
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/17.3.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set.has
460671392.0 Ops/sec
key in
366664608.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **What is being tested?** The benchmark compares two approaches to check if a value exists in an object or a Set data structure: 1. `set.has()`: This method is part of the built-in `Set` object in JavaScript, which allows you to check if a specific value exists within the set. 2. `"9 in b"`: This syntax uses the "in" operator to check if a value (in this case, `9`) is present as a key in an object (`b`). **Options compared** The two options being compared are: * `set.has()`: This method is fast and efficient because it uses a hash table to store the set's elements. Looking up a value in a set involves a constant-time operation (O(1)). * `"9 in b"`: This syntax involves iterating over the object's keys using the "in" operator, which has an average time complexity of O(1) for modern browsers, but can be slower than `set.has()` due to additional overhead. **Pros and Cons** * `set.has()`: + Pros: - Fast and efficient (O(1)) - Built-in method with minimal overhead + Cons: - Only supported in modern browsers that support the Set object - May not work as expected in older browsers or environments without a Set object * `"9 in b"`: + Pros: - Works in most browsers and environments - Doesn't rely on any specific libraries or APIs + Cons: - Slower than `set.has()` (O(1) average, but can be slower due to additional overhead) - More verbose syntax **Library** In the provided benchmark code, there is no explicit library being used. However, it's worth noting that if you were to use a library like Lodash or Underscore.js, which provide utility functions for working with objects and sets, they might offer implementations of `set.has()`-like functionality. **Special JS feature** There are no special JavaScript features or syntax being tested in this benchmark. It's a straightforward comparison between two common approaches to checking if a value exists in an object or set. **Other alternatives** If you're looking for alternative ways to check if a value exists in an object, some other options include: * Using the `Object.prototype.hasOwnProperty.call()` method: This is another way to check if a property exists on an object. * Using a library like Lodash's `_.has()` function: This function provides a flexible and efficient way to check if a value exists in an object or array. Keep in mind that the best approach often depends on your specific use case, performance requirements, and target browser support.
Related benchmarks:
set.has vs. Object key lookup for real
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key
set.has vs. Object key lookup2
Comments
Confirm delete:
Do you really want to delete benchmark?