Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object key lookup for real without bang bang
(version: 0)
Comparing performance of:
set.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:
set.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
set.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
set.has
497031648.0 Ops/sec
key lookup
291736704.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and discussed. **Benchmark Definition JSON** The benchmark definition provides two scripts that are used to test different approaches for finding an element in a Set (a collection of unique values) versus looking up a key in an Object. 1. **Script Preparation Code**: This code creates a new Set `a` containing numbers from 1 to 10, and another object `b` with the same keys as the set but with values that indicate presence (true) or absence (false). 2. **Html Preparation Code**: There is no HTML preparation code provided in this benchmark definition. **Individual Test Cases** The test cases are designed to measure the performance of two different approaches: 1. **Test Name: "set.has"**: This test case uses the `has()` method on the Set `a` to check if a specific value (9) is present. 2. **Test Name: "key lookup"**: This test case uses object property access (`b['9']`) to look up the value associated with key 9 in the object `b`. **Comparison** In this benchmark, we have two approaches being compared: * Using the `has()` method on a Set * Looking up a key in an Object using bracket notation The test cases aim to measure which approach is faster and more efficient for finding a specific value or checking presence in a collection. **Pros and Cons of Each Approach** 1. **Set.has() Method**: Pros: * Fast lookup with average O(1) time complexity * Can be used to check for presence (i.e., if an element exists in the Set) Cons: * Not designed for key-value lookups; might require additional operations to achieve this 2. **Object Key Lookup**: Pros: * Can be used for key-value lookups * Allows accessing values associated with specific keys Cons: * Slower lookup with average O(1) time complexity, especially if the object is large or has many keys * May not provide an immediate indication of presence (i.e., checking `b['9']` might return undefined if the key doesn't exist) **Library and Syntax** The Set data structure and its methods (`has()`) are built into JavaScript. No additional library is required for this benchmark. No special JS features or syntax are used in this benchmark. **Other Alternatives** If you need to find a specific value in a collection, consider using other data structures like: * Arrays (using `indexOf()` or a binary search algorithm) * Maps (if you need key-value lookups with fast access times) For checking presence in a collection, Set is still an efficient and recommended approach. Keep in mind that the choice of approach depends on your specific use case and requirements.
Related benchmarks:
set.has(key) vs. Boolean(obj[key])
set.has vs. Object key lookup for real
set.has vs. Object key
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?