Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object key lookup 2
(version: 0)
Comparing performance of:
includes vs key lookup
Created:
3 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:
includes
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
includes
key lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
1126575232.0 Ops/sec
key lookup
1084641536.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares two approaches to retrieve values from a Set and an object using different methods: `has` on the Set and key lookup (indexing) on the object. **Options Compared** 1. **Set.has**: This method uses the `has()` function on a Set, which checks if a value exists in the Set. 2. **Object Key Lookup**: This approach directly accesses an object using its key (e.g., `b['9']`). **Pros and Cons of Each Approach** * **Set.has** * Pros: * Faster, as it uses a hash table to quickly determine if a value exists. * More efficient for large Sets. * Cons: * May be slower for very small Sets due to the overhead of creating a hash table. * Requires a Set data structure. * **Object Key Lookup** * Pros: * Generally faster than using `has()` on a Set, as it only requires accessing an index. * Does not require a Set data structure. * Cons: * May be slower for large Sets or dynamic objects, as it needs to traverse the object's keys. **Library and Syntax Used** * **Set**: The `Set` data structure is used in the benchmark. A Set is a collection of unique values that can be stored in JavaScript. * **No special syntax or features are required for this benchmark**, but JavaScript itself is a dynamic language with its own set of features, such as closures and async/await, which may influence performance under certain conditions. **Alternatives** Other alternatives to compare could include: 1. **Array.prototype.includes()**: This method would allow comparing the performance of using an array's `includes()` function versus accessing keys directly on the object or Set. 2. A dynamic object comparison approach might test the speed of looking up key-value pairs in a JavaScript object, perhaps using a library like Lodash. 3. Comparison of direct property access vs. indirect access (e.g., calling a method that returns an object property). 4. **For loop** vs **Array.prototype.forEach()**: Benchmarking iterating over arrays or collections. **Benchmark Preparation Code** The provided script preparation code creates two variables: * `a`: A new Set containing the numbers 1-10. * `b`: An object with the same keys as the Set, but initialized to a boolean value of true for each key. The HTML preparation code is not necessary in this case, as no HTML is required for the benchmark.
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
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?