Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Published
7 min read
Map and Set in JavaScript
S
I write code , that run in the browser and someone else's machine. And sometimes I also write articles

As JavaScript applications grow in complexity, choosing the correct data structure becomes extremely important. While Objects and Arrays are the most commonly used data structures, they were not designed to solve every storage problem efficiently.

To address certain limitations, JavaScript introduced two specialized data structures:

  • Map

  • Set

To properly understand them, we need to explore:

  • What problems they solve

  • How they differ from traditional Objects and Arrays

  • When they are the better choice

  • How they behave internally

  • Real-world scenarios where they shine

Let us build this understanding carefully and thoroughly.


Understanding the Limitations of Objects

Objects are traditionally used for key-value storage.

const user = {
  name: "Alice",
  age: 25
};

At first glance, objects seem perfect. But they have hidden limitations.

Problem 1: Keys Are Always Strings (or Symbols)

const obj = {};
obj[1] = "Number key";
obj["1"] = "String key";

console.log(obj);

Both keys become "1" internally.

JavaScript automatically converts object keys to strings. This can cause subtle bugs when you expect number keys to behave differently.


Problem 2: No Built-in Size Property

To count properties in an object:

Object.keys(obj).length;

This requires extra processing.

Maps, however, have a direct .size property.


Problem 3: Objects Were Not Designed for Frequent Add/Delete Operations

Objects are optimized primarily for structured data, not dynamic insertion and deletion like hash tables.

When you frequently add and remove keys, Map performs better in most JavaScript engines.


Problem 4: Iteration Complexity

Iterating over objects requires special methods:

for (let key in obj) {
  console.log(key, obj[key]);
}

But this can also iterate over inherited properties unless filtered.

Map iteration is cleaner and more predictable.


What Is Map

A Map is a collection of key-value pairs where:

  • Keys can be any data type

  • Order of insertion is preserved

  • It is optimized for frequent updates

Think of Map as a true hash map implementation.


Creating and Using a Map

const map = new Map();

map.set("name", "Alice");
map.set(42, "Number Key");

console.log(map.get("name")); // Alice
console.log(map.get(42));     // Number Key

Unlike objects, 42 remains a number key.


Using Objects as Keys

This is where Map becomes powerful.

const user1 = { id: 1 };
const user2 = { id: 2 };

const userMap = new Map();

userMap.set(user1, "Admin");
userMap.set(user2, "Guest");

console.log(userMap.get(user1)); // Admin

Each object is treated as a unique reference.

In a normal object:

const obj = {};
obj[user1] = "Admin";

This would convert user1 into "[object Object]", which is problematic.


Map Internal Behavior Insight

Maps:

  • Store keys using strict equality comparison

  • Preserve insertion order

  • Provide near constant-time lookup

This makes Map ideal for:

  • Caching systems

  • Frequency counting

  • Object reference tracking

  • Dynamic configuration storage


Iterating Over Map

const map = new Map([
  ["a", 1],
  ["b", 2],
  ["c", 3]
]);

for (let [key, value] of map) {
  console.log(key, value);
}

Map provides clean iteration without worrying about inherited properties.


Map vs Object Deep Comparison

Feature Object Map
Key Types String or Symbol Any data type
Maintains Insertion Order Not guaranteed historically Guaranteed
Size Property No Yes (.size)
Iteration Indirect Direct
Performance for Add/Delete Slower Faster
Use Case Structured data Dynamic key-value store

Understanding the Limitations of Arrays

Arrays are ordered lists.

const numbers = [1, 2, 3];

Arrays allow duplicates:

const numbers = [1, 2, 2, 3];

Sometimes duplicates are unwanted.

To remove duplicates manually:

const unique = numbers.filter((value, index) => numbers.indexOf(value) === index);

This is inefficient and less readable.


What Is Set

A Set is a collection of unique values.

Key characteristics:

  • No duplicate values allowed

  • Maintains insertion order

  • Fast lookup for existence


Creating and Using Set

const set = new Set();

set.add(1);
set.add(2);
set.add(2);

console.log(set); // Set(2) {1, 2}

Duplicate values are automatically ignored.


Removing Duplicates from Array

One of the most common patterns:

const numbers = [1, 2, 2, 3, 4, 4];

const uniqueNumbers = [...new Set(numbers)];

console.log(uniqueNumbers);

Step-by-step explanation:

  1. new Set(numbers) removes duplicates.

  2. Spread operator converts Set back into array.

This is clean and efficient.


Checking Membership Quickly

Set is optimized for existence checks.

const ids = new Set([101, 102, 103]);

console.log(ids.has(102)); // true

Checking .has() in Set is generally faster than .includes() in large arrays.


Iterating Over Set

const set = new Set(["A", "B", "C"]);

for (let value of set) {
  console.log(value);
}

Iteration order matches insertion order.


Set vs Array Deep Comparison

Feature Array Set
Allows Duplicates Yes No
Indexed Access Yes No
Built-in Uniqueness No Yes
Fast Lookup Slower Faster
Best For Ordered data with duplicates Unique collections

Real-World Use Cases

1. Tracking Unique Visitors

const visitors = new Set();

function visit(user) {
  visitors.add(user);
}

visit("Alice");
visit("Bob");
visit("Alice");

console.log(visitors.size); // 2

Ensures no duplicate entries.


2. Counting Frequency with Map

const words = ["apple", "banana", "apple", "orange"];

const frequency = new Map();

for (let word of words) {
  frequency.set(word, (frequency.get(word) || 0) + 1);
}

console.log(frequency);

Map is perfect for counting because:

  • Keys can be anything

  • Efficient lookup and update


3. Caching Expensive Calculations

const cache = new Map();

function expensiveCalculation(num) {
  if (cache.has(num)) {
    return cache.get(num);
  }

  const result = num * num;
  cache.set(num, result);

  return result;
}

Map acts as a simple memoization storage.


Choosing Between Them

Use Map when:

  • Keys are not strings

  • Order matters

  • Frequent add/delete operations occur

  • You need reliable iteration

Use Set when:

  • You need uniqueness

  • You need fast membership checks

  • Removing duplicates from arrays


Mental Model Summary

Think of them this way:

  • Object → Basic structured storage

  • Map → Advanced dynamic key-value storage

  • Array → Ordered list

  • Set → Ordered list with automatic uniqueness

Choosing the right structure improves performance, readability, and correctness.


Conclusion

Map and Set are powerful additions to JavaScript that solve real limitations of Objects and Arrays.

Map provides flexible and efficient key-value storage with support for any key type and better performance for dynamic operations.

Set ensures uniqueness automatically and offers fast membership checks, making it ideal for handling collections of distinct values.

Understanding when and why to use Map and Set allows you to write more efficient, cleaner, and more scalable JavaScript code.

Map and Set in JavaScript