Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs obj[key] vs map.get
(version: 0)
Comparing performance of:
includes vs hash vs set vs map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(10000).fill(undefined).map((_, i) => i); var b = a.reduce((acc, el) => { acc[el] = true; return acc }, {}); var c = new Set(a); var d = new Map(a.map(el => [el, true]));
Tests:
includes
return a.includes(9)
hash
return b['9']
set
return c.has(9)
map
return d.get(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
hash
set
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
25697368.0 Ops/sec
hash
44145224.0 Ops/sec
set
45429340.0 Ops/sec
map
43847180.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark json and explain what is being tested, compared, and its pros and cons. **Benchmark Definition** The benchmark definition represents four different ways to check if a value (in this case, 9) exists in an array: 1. `a.includes(9)`: Checks if the array `a` contains the value 9 using the `includes()` method. 2. `b['9']`: Accesses the value associated with key 9 in the object `b`. 3. `c.has(9)`: Checks if the set `c` contains the value 9 using the `has()` method. 4. `d.get(9)`: Retrieves the value associated with key 9 in the map `d`. **Comparison** These four methods are being compared to determine which one is the fastest. **Pros and Cons of each approach:** 1. **`a.includes(9)`**: * Pros: Simple, widely supported, and efficient for large arrays. * Cons: May be slower than other approaches if the array has many unique elements. 2. **`b['9']`**: * Pros: Fast and efficient for small objects with a limited number of keys. * Cons: Can be slow for large objects or when using a hash table-like data structure (like `Map`). 3. **`c.has(9)`**: * Pros: Efficient for sets, which are optimized for fast membership testing. * Cons: May not be as efficient as other approaches for arrays without duplicates. 4. **`d.get(9)`**: * Pros: Fast and efficient for maps, which are optimized for fast key lookups. * Cons: May not be as efficient as other approaches for objects with a large number of keys. **Special JS feature or syntax:** None of the provided methods use special JavaScript features or syntax that would require additional explanation. **Alternative approaches:** Other alternatives to these methods could include: 1. Using `Array.prototype.indexOf()` instead of `includes()`. 2. Using a custom loop or function to iterate over the array or object. 3. Using a more specialized data structure, such as a trie or suffix tree, for fast membership testing. However, these alternative approaches are not being tested in this benchmark. **Other considerations:** 1. The benchmark is running on a desktop environment with Chrome 125 and Mac OS X 10.15.7. 2. The test cases are designed to be representative of real-world use cases, but the results may vary depending on the specific requirements of your application. 3. It's worth noting that the `Map` implementation in JavaScript is still evolving and may not be optimized for all use cases. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
set.has vs. array.includes 2
Flatmap vs reduce with objects
Array.includes vs Set.has vas Map.has 2
Array.includes vs Set.has vas Map.has with large data set
Comments
Confirm delete:
Do you really want to delete benchmark?