Set (initial support)
Baseline Widely available
Supported in Chrome: yes.
Supported in Edge: yes.
Supported in Firefox: yes.
Supported in Safari: yes.
This feature is well established and works across many devices and browser versions. Itβs been available across browsers since July 2015
Map (initial support)
Baseline Widely available
Supported in Chrome: yes.
Supported in Edge: yes.
Supported in Firefox: yes.
Supported in Safari: yes.
This feature is well established and works across many devices and browser versions. Itβs been available across browsers since July 2015
Before ES6 introduced Sets and Maps, we had limited options for storing collections of data in JavaScript. We used objects for key-value pairs and arrays for lists. This led to common problems:
What are Sets and Maps?
Sets and Maps are specialized data structures in JavaScript, each designed to solve specific problems that arrays and objects handle poorly.
A Set is a collection of unique values. Think of it like a bag that automatically removes duplicates.
When you add the same value twice, the Set keeps only one copy. Itβs perfect for maintaining lists where each item should appear only once.
A Map is a collection of key-value pairs where keys can be any type - numbers, strings, objects, even functions. Unlike objects, which convert all keys to strings, Maps preserve the type of the key. This makes them ideal for creating dictionaries or caches where the key type matters:
Hereβs how Sets and Maps solve the two problems we saw earlier with arrays and objects.
Sets and Maps excel at handling data relationships, caching, and uniqueness checks. Each has specific use cases where they outperform traditional arrays and objects.
When to Use Sets
Sets shine when you need fast lookups and uniqueness guarantees in your data. In a tag system, you can instantly check if an article has a specific tag without looping through an array.
When to Use Maps
Maps are perfect when you need to associate data with any type of key - like caching API responses by URL, storing user preferences, or maintaining a relationship between DOM elements and their data.
Performance Trade-offs
Sets and Maps provide O(1) operations through their hash table implementation, compared to arrays which have O(n) for lookups and objects which coerce keys to strings.
O(1) means an operation takes the same amount of time regardless of how much data you have - itβs βconstant timeβ. If you have 10 items or 10 million items in your Set or Map, operations like adding, checking, or retrieving items will be equally fast.
O(n) means the operation time grows linearly with the size of your data. If you have 1000 items, it might check 1000 times. If you have a million items, it might check a million times. The more data you have, the slower it gets.
However, this speed comes at a cost: Sets and Maps use more memory than arrays and objects. The hash table structure that enables their fast operations requires additional memory overhead to maintain.
For Sets specifically, the hash table ensures that duplicate detection is instantaneous, rather than requiring a full array scan. Maps achieve their performance by using a similar structure but storing both the key and value in the hash table.
Sets and Maps excel at handling user sessions. Sets provide instant O(1) lookups to check if a user is logged in - much faster than searching through an array of user IDs. Maps let us store session data with any type of user identifier (number, string, or object) without key type conversion issues.
The SessionManager
example shows how Sets can track unique active users while Maps store detailed session data. This combination is particularly powerful because it separates concerns - the Set handles uniqueness while the Map handles data association.
The instant lookup times make this ideal for high-traffic applications where performance is critical.
When Not to Use Sets and Maps
Sets and Maps come with memory overhead from their hash table structure. For small collections or simple string keys, arrays and objects might be more efficient.
Sets and Maps arenβt just alternatives to arrays and objects - theyβre specialized tools that make specific programming patterns more efficient and reliable. Use them when their unique characteristics align with your needs, and youβll write more robust and performant code.
Key takeaway: Sets solve the unique values problem, while Maps solve the key-type coercion problem. They trade more memory usage for better performance and type safety.