Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object lookup
(version: 0)
Comparing performance of:
set.has vs Object 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)
Object 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
Object lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set.has
35324144.0 Ops/sec
Object lookup
33958308.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what's being tested. **Benchmark Definition** The test case measures the performance difference between using `Set.has()` (a built-in JavaScript method) versus checking for a key in an object using the unary `!!` operator (an alternative way to check if a property exists). **Options Compared** The two options being compared are: 1. **`Set.has()`**: This is a built-in method provided by the JavaScript `Set` data structure, which allows you to check if a specific value (in this case, `9`) exists in the set. 2. **Object Lookup using `!!`**: This alternative approach involves using the unary `!!` operator to check if the property `9` exists in the object `b`. **Pros and Cons** * **`Set.has()`**: + Pros: Fast and efficient, as it uses a hash table data structure under the hood. + Cons: Not all browsers support this method (only Chrome and some other modern browsers). * **Object Lookup using `!!`**: + Pros: Widely supported by most browsers, including older ones. + Cons: May be slower than `Set.has()` due to the need to iterate over the object's properties. **Library Used** In this benchmark, no specific library is used. The test relies solely on built-in JavaScript features and data structures (the `Set` and object). No special JavaScript features or syntax are being tested in this case. **Other Alternatives** If you wanted to test alternative approaches for checking if a value exists in an array, you could consider the following: * Using the `includes()` method: This is another built-in method provided by arrays in modern browsers. * Using a custom iterative approach: You could write your own function that iterates over the array and checks each element until it finds a match or exhausts the entire array. For example: ```javascript function exists(arr, value) { for (let i = 0; i < arr.length; i++) { if (arr[i] === value) return true; } return false; } ``` This alternative approach would likely be slower than `Set.has()` but could provide more flexibility and control over the iteration process. Overall, this benchmark provides a simple yet informative test case that highlights the performance differences between using built-in methods versus traditional object lookup approaches in JavaScript.
Related benchmarks:
set.has vs. Object key lookup 2
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?