Maps & Sets

In the previous archive, we learnt that standard Objects have a fatal flaw. They implicitly convert all keys to Strings. ES6 introduced proper data structures to solve this architectural limitation.

A. The Map

A Map is a collection of keyed data items, similar to an Object. However, the main difference is that Map allows keys of any type.

  • No Coercion: We can use an Object, a Function, or a Number as a key. The engine does not convert them to strings.
  • Reference Equality: It uses the memory address of the key to identify it. Two different empty objects {} are treated as two unique keys.
  • Ordered: Unlike Objects (which have complex ordering rules), a Map guarantees that entries are iterated in the exact order they were inserted.

Example:

let map = new Map();
let user = { name: "Singh" };
 
// Using an Object as a key
map.set(user, "Architect");
 
console.log(map.get(user)); // "Architect"
console.log(map.size); // 1

B. The Set

A Set is a special collection of set of values (without keys) where each value may occur only once.

  • Uniqueness: If we try to add the same value twice, the Set automatically ignores the duplicate.
  • Architecture: It is optimized for checking presence. Checking set.has(value) is significantly faster (O(1)) than searching an Array arr.includes(value) (O(N)).

Example:

let mySet = new Set();
 
mySet.add(1);
mySet.add(5);
mySet.add(1); // Ignored (Duplicate)
 
console.log(mySet.size); // 2

📝 Summary Table

FeatureObjectMapSet
ConceptRecord / DictionaryTrue Hash MapUnique List
Key TypesStrings / SymbolsAny TypeN/A (Values only)
Key CoercionYes (Implicit String)NoN/A
DuplicatesOverwrites KeyUnique KeysUnique Values
IterationObject.keys().keys(), for..offor..of
SizeManual Count.size.size