Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object key
(version: 0)
Comparing performance of:
has vs key lookup
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:
has
return a.has(9)
key lookup
return !!b['9']
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
has
key lookup
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
has
484575872.0 Ops/sec
key lookup
473972352.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: `Set.has()` and object key lookup (`!!b['9']`). The test case uses JavaScript, which provides several features that allow for concise and expressive code. **Options Compared** There are two options compared: 1. **`Set.has()`**: This method checks if a specific value exists in a Set data structure. 2. **Object key lookup**: This approach uses the `!!b['9']` syntax to check if a property (in this case, the string `'9'`) exists as a key in an object (`b`). **Pros and Cons of Each Approach** 1. **`Set.has()`**: * Pros: + Faster lookup times since Sets are optimized for efficient membership testing. + More concise code compared to traditional object key lookup. * Cons: + May require additional memory allocation if the Set grows dynamically. + Not all browsers support Sets (although most modern ones do). 2. **Object Key Lookup (`!!b['9']`)**: * Pros: + More familiar and widely supported syntax across different browsers and platforms. + No additional memory allocation required. * Cons: + Slower lookup times compared to `Set.has()`, especially for large datasets. **Library/Feature** The benchmark uses the JavaScript `Set` object, which provides an efficient way to store unique values. The `has()` method is a part of this API. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. However, it's worth noting that the use of the nullish coalescing operator (`??`) is not present in this example, but it's sometimes used to simplify object key lookups by providing a default value if the property doesn't exist. **Other Alternatives** If you wanted to explore alternative approaches for this benchmark, here are a few options: 1. **Array.includes()**: This method checks if a specific value exists within an array. 2. **Object.prototype.hasOwnProperty()**: This method checks if a property exists as a direct property of an object (not inherited from prototype chains). 3. **Array.indexOf() / Array.lastIndexOf()**: These methods search for the first or last occurrence of a specified value in an array. Keep in mind that each alternative approach has its own trade-offs, such as performance characteristics and code readability. For this specific benchmark, using `Set.has()` provides a clear performance advantage over object key lookup (`!!b['9']`) on modern browsers.
Related benchmarks:
set.has(key) vs. Boolean(obj[key])
set.has vs. Object key lookup for real
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?