Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.has vs Set.has 01.04.2025
(version: 1)
Comparing performance of:
Set.has vs Map.has
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const set = new Set(["one", "two"]); const map = new Map([["one", true], ["two", true]]);
Tests:
Set.has
set.has('one'); set.has('two'); set.has('three');
Map.has
map.has('one'); map.has('two'); map.has('three');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set.has
Map.has
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/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set.has
161844352.0 Ops/sec
Map.has
169454928.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of two JavaScript data structures: `Set` and `Map`. Both `.has` methods of these structures are compared to determine which is more efficient in checking for the existence of elements. ### Overview of Tested Options 1. **Set.has**: - The `Set` data structure is designed to store unique values of any type. - The `.has(value)` method returns `true` if the value exists in the `Set`, and `false` otherwise. 2. **Map.has**: - The `Map` data structure holds key-value pairs. - The `.has(key)` method returns `true` if an element with the specified key exists in the `Map`, and `false` otherwise. ### Pros and Cons of Each Approach #### Pros of Set.has: - **Performance**: Generally optimized for existence checks due to its unique value focus, potentially offering faster lookups for large sets of unique items. - **Memory Efficiency**: Uses less memory overhead compared to `Map` when only unique keys are needed, as it only tracks the value. #### Cons of Set.has: - **Limited Functionality**: Only stores values without associated metadata or data structures. #### Pros of Map.has: - **Key-Value Storage**: Allows for more complex data storage, associating each key with a specific value, making it suitable for scenarios requiring both keys and values. - **Direct Lookup by Key**: Usually offers a very efficient lookup mechanism for large datasets involving keys linked to values. #### Cons of Map.has: - **Memory Overhead**: Slightly more memory-intensive due to the storage of key-value pairs compared to the simple collection of unique values in `Set`. ### Other Considerations - The choice between `Set` and `Map` depends heavily on the specific use case. For pure existence checks, where you only need to know whether an item is present, `Set` may be more appropriate. However, if you need to associate the existence of a value with some additional information (i.e., a key-value relationship), `Map` would be the better choice. - Both methods are part of the ECMAScript 2015 (ES6) specification, which means they are well-supported across modern browsers. ### Benchmark Results From the latest benchmark results: - `Map.has` executed **169,454,928 times per second**. - `Set.has` executed **161,844,352 times per second**. This shows that, according to the benchmark conducted in the specified environment (Chrome 134 on macOS), `Map.has` offers a slightly better performance compared to `Set.has`. However, the performance difference is marginal, and the choice should be guided by the intended use case rather than the minuscule performance difference in this specific scenario. ### Alternatives Alternatives to using `Set` and `Map` for similar functionality can include: - **Arrays**: While you can use arrays to store unique values, checking for existence (`array.includes(value)`) is generally less efficient than `Set` and `Map`, especially with larger datasets. - **Object Literals**: For situations where key-value pairing is required. However, `Map` is preferred due to better performance, especially for large datasets and when using non-string keys. - **WeakSet and WeakMap**: These structures allow you to store weakly held references to objects, which means they do not prevent garbage collection, providing a unique use case compared to `Set` and `Map`. In summary, the benchmark provides useful insights into the performance of two foundational data structures in JavaScript, helping developers make informed decisions based on their requirements.
Related benchmarks:
Set vs Object vs Map access
Set vs Object vs Map add +access
Map get VS Map has get2
Map get VS Map has get3
Map.has vs Map.get
Map.has vs Map.get check if the key exists
Set vs Object vs Map 2
Set vs Object vs Map 3
Map.has vs Set.has vs Array.includes 01.04.2025
Comments
Confirm delete:
Do you really want to delete benchmark?