Skip to main content

Command Palette

Search for a command to run...

Mastering Map and Set in JavaScript

Updated
•5 min read•View as Markdown
Mastering Map and Set in JavaScript
S
I like to make complex technical topics easy.

Introduction

When storing collections of data in JavaScript, developers typically reach for Objects or Arrays. While these built-in data structures are extremely versatile, they come with subtle limitations that can lead to performance bottlenecks, bug-prone code, or awkward workarounds.

To address these pain points, ES6 (ECMAScript 2015) introduced two specialized data structures: Map and Set.

In this article, we’ll explore what Map and Set are, how they solve traditional problems with Objects and Arrays, and when you should use them in your applications.

The Limitations of Traditional Objects and Arrays

Before diving into Map and Set, let's look at why standard Objects and Arrays aren't always ideal.

1. The Limitations of Objects as Key-Value Stores

  • Key Type Restriction: Object keys are automatically converted to strings or symbols. If you use an object or number as a key, JavaScript implicitly converts it using .toString().

  • Prototype Pollution & Default Keys: Objects inherit properties from Object.prototype. Unintended key collisions can occur (e.g., if a key is named 'toString' or 'constructor').

  • Unpredictable Iteration Order: Although modern JavaScript preserves key order in many scenarios, relying on object key order isn't as straightforward or guaranteed as true ordered collections.

  • Size Calculation: Finding the number of items in an object requires Object.keys(obj).length, which runs in O(N) time instead of O(1).

2. The Limitations of Arrays for Unique Collections

  • Duplicate Values: Arrays willingly store duplicate elements. Ensuring uniqueness requires manual filtering or helper methods.

  • Expensive Searches: Checking if an item exists in an array via .includes() or .indexOf() takes linear time O(N), which slows down significantly as datasets grow.

What is Map?

A Map is an ordered collection of key-value pairs where keys can be of any data type—including objects, functions, primitive types, or even other Maps.

Key Features & Syntax

// Creating a Map
const userRoles = new Map();

// Setting key-value pairs
const user1 = { name: "Alice" };
const user2 = { name: "Bob" };

userRoles.set(user1, "Admin");
userRoles.set(user2, "Editor");
userRoles.set(100, "Numeric Key Example");

// Accessing values
console.log(userRoles.get(user1)); // Output: "Admin"
console.log(userRoles.has(100));    // Output: true

// Getting size
console.log(userRoles.size);        // Output: 3

// Deleting keys
userRoles.delete(100);

Visualizing Map Key-Value Storage

┌───────────────────────────────────────────┐
│                    MAP                    │
├───────────────────┬───────────────────────┤
│        KEY        │         VALUE         │
├───────────────────┼───────────────────────┤
│ { name: "Alice" } │ ───► "Admin"          │
│ { name: "Bob" }   │ ───► "Editor"         │
│ 100               │ ───► "Numeric Key..." │
└───────────────────┴───────────────────────┘

What is Set?

A Set is a collection of unique values. Unlike arrays, a Set does not allow duplicate entries; adding an existing value to a Set simply does nothing.

Key Features & Syntax

// Creating a Set
const uniqueTags = new Set();

// Adding elements
uniqueTags.add("javascript");
uniqueTags.add("webdev");
uniqueTags.add("javascript"); // Duplicate addition ignored

console.log(uniqueTags); // Output: Set(2) { 'javascript', 'webdev' }

// Quick Existence Check (O(1) complexity)
console.log(uniqueTags.has("webdev")); // Output: true

// Removing duplicate items from an array in one line
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]

Visualizing Set Uniqueness

INPUT DATA            SET STORAGE
┌─────────────┐       ┌─────────────┐
│ "js"        │ ────► │ "js"        │
│ "react"     │ ────► │ "react"     │
│ "js" (dup)  │ ─x──► │ (ignored)   │
│ "node"      │ ────► │ "node"      │
└─────────────┘       └─────────────┘

Difference Between Map and Object

Feature

Object

Map

Key Types

Strings or Symbols only

Any data type (Objects, Functions, Primitives)

Direct Size Property

No (Object.keys(obj).length)

Yes (map.size)

Element Order

Complex / Keys ordered by spec

Strict Insertion Order preserved

Performance

Optimized for small, fixed structure lookups

Optimized for frequent addition/deletion of key-value pairs

Default Keys

Inherits from Object.prototype

Pure collection (no default inherited keys)

Iteration

Requires Object.keys() or for...in

Directly iterable (for...of, .forEach())

Difference Between Set and Array

Feature

Array

Set

Duplicate Values

Allowed

Strictly forbidden (Unique only)

Element Access

By index (e.g., arr[0])

No index lookup; iterate or check existence

Lookup Time ($O$)

$O(N)$ using .includes()

$O(1)$ constant time using .has()

Order

Maintained by explicit index

Maintained by insertion order

Use Case

Ordered lists with potential duplicates

Unique collections, membership testing

When to Use Map and Set

When to use Map:

  1. Keys are unknown at runtime or dynamic (e.g., mapping DOM elements or user object references to metadata).

  2. Keys are non-strings, such as numbers, objects, or functions.

  3. Frequent additions and deletions occur in performance-critical scenarios.

  4. Guaranteed insertion order iteration is required.

When to use Set:

  1. Deduplicating data (e.g., removing duplicates from user input or database queries).

  2. Fast membership checks (.has()) on large datasets where linear array searches create UI lag.

  3. Tracking unique user interactions (e.g., unique visitor IDs, active tags, or checked permissions).

Summary

  • Use Map when you need a robust dictionary/hash-map with non-string keys, guaranteed insertion order, and fast size checks.

  • Use Set when you need a collection of guaranteed unique values with instant lookup capability.

1 views