Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Updated
6 min readView as Markdown
Map and Set in JavaScript
D

I am a developer learning web development , I am a college dropout pursuing my passion in software field

For years, JavaScript developers relied almost exclusively on Objects for key-value storage and Arrays for lists. While they served us well, they came with "quirks" that often led to verbose code or unexpected bugs.

With the arrival of ES6, we got two powerful specialized collections: Map and Set. If you've ever struggled with an Object turning your keys into strings or an Array filled with unwanted duplicates, these are the tools you've been waiting for.

The Limitations of the "Old Guard"

Before we dive into the new features, let's look at why Objects and Arrays sometimes fall short:

  1. Object Key Stringification: Objects only support strings or symbols as keys. If you try to use another object as a key, it gets converted to the string "[object Object]".

  2. Object Boilerplate: Checking the size of an object requires Object.keys(obj).length, which is inefficient.

  3. Array Duplicates: Arrays don't care about uniqueness. If you need a list of unique IDs, you have to manually write logic to check for existence before pushing.

Modern Collections: Mastering Map and Set in JavaScript

For years, JavaScript developers relied almost exclusively on Objects for key-value storage and Arrays for lists. While they served us well, they came with "quirks" that often led to verbose code or unexpected bugs.

With the arrival of ES6, we got two powerful specialized collections: Map and Set. If you've ever struggled with an Object turning your keys into strings or an Array filled with unwanted duplicates, these are the tools you've been waiting for.


The Limitations of the "Old Guard"

Before we dive into the new features, let's look at why Objects and Arrays sometimes fall short:

  1. Object Key Stringification: Objects only support strings or symbols as keys. If you try to use another object as a key, it gets converted to the string "[object Object]".

  2. Object Boilerplate: Checking the size of an object requires Object.keys(obj).length, which is inefficient.

  3. Array Duplicates: Arrays don't care about uniqueness. If you need a list of unique IDs, you have to manually write logic to check for existence before pushing.


1. JavaScript Map: The Supercharged Key-Value Store

A Map is a collection of keyed data items, similar to an Object. However, the "Superpower" of a Map is that it allows keys of any type—functions, objects, or even primitives.

Why use Map over Object?

Feature

Object

Map

Key Types

Strings and Symbols only

Any type (Objects, Functions, etc.)

Size

Manual calculation

.size property (O(1) complexity)

Performance

Better for small, static metadata

Better for frequent additions/removals

Iteration

Use for...in (with caution)

Directly iterable (guaranteed order)

const userRoles = new Map();

const admin = { name: "Alice" };
const editor = { name: "Bob" };

// Using objects AS keys!
userRoles.set(admin, "Superuser");
userRoles.set(editor, "Content-Only");

console.log(userRoles.get(admin)); // "Superuser"
console.log(userRoles.size); // 2

2. JavaScript Set: The Collection of Uniques

A Set is a collection of values where each value must be unique. It effectively eliminates the "duplicate problem" by design.

Why use Set over Array?

While Arrays are ordered lists meant for indexing, Sets are meant for membership checks.

Feature

Array

Set

Uniqueness

Allows duplicates

Guaranteed unique

Search Speed

$O(n)$ (Slow for large lists)

$O(1)$ (Extremely fast)

Access

Via index (arr[0])

Via value checking (has(val))

Modern Collections: Mastering Map and Set in JavaScript

For years, JavaScript developers relied almost exclusively on Objects for key-value storage and Arrays for lists. While they served us well, they came with "quirks" that often led to verbose code or unexpected bugs.

With the arrival of ES6, we got two powerful specialized collections: Map and Set. If you've ever struggled with an Object turning your keys into strings or an Array filled with unwanted duplicates, these are the tools you've been waiting for.


The Limitations of the "Old Guard"

Before we dive into the new features, let's look at why Objects and Arrays sometimes fall short:

  1. Object Key Stringification: Objects only support strings or symbols as keys. If you try to use another object as a key, it gets converted to the string "[object Object]".

  2. Object Boilerplate: Checking the size of an object requires Object.keys(obj).length, which is inefficient.

  3. Array Duplicates: Arrays don't care about uniqueness. If you need a list of unique IDs, you have to manually write logic to check for existence before pushing.


1. JavaScript Map: The Supercharged Key-Value Store

A Map is a collection of keyed data items, similar to an Object. However, the "Superpower" of a Map is that it allows keys of any type—functions, objects, or even primitives.

Why use Map over Object?

Feature

Object

Map

Key Types

Strings and Symbols only

Any type (Objects, Functions, etc.)

Size

Manual calculation

.size property (O(1) complexity)

Performance

Better for small, static metadata

Better for frequent additions/removals

Iteration

Use for...in (with caution)

Directly iterable (guaranteed order)

Quick Example:

JavaScript

const userRoles = new Map();

const admin = { name: "Alice" };
const editor = { name: "Bob" };

// Using objects AS keys!
userRoles.set(admin, "Superuser");
userRoles.set(editor, "Content-Only");

console.log(userRoles.get(admin)); // "Superuser"
console.log(userRoles.size); // 2

2. JavaScript Set: The Collection of Uniques

A Set is a collection of values where each value must be unique. It effectively eliminates the "duplicate problem" by design.

Why use Set over Array?

While Arrays are ordered lists meant for indexing, Sets are meant for membership checks.

Feature

Array

Set

Uniqueness

Allows duplicates

Guaranteed unique

Search Speed

$O(n)$ (Slow for large lists)

$O(1)$ (Extremely fast)

Access

Via index (arr[0])

Via value checking (has(val))

The "Magic" of Uniqueness:

One of the most common uses for a Set is to instantly remove duplicates from an Array.

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];

console.log(uniqueNumbers); // [1, 2, 3, 4, 5]

When to Use Which?

Choosing the right collection can significantly improve your code's performance and readability.

Use a Map when:

  • You need keys that are not strings (e.g., mapping a DOM element to some metadata).

  • You are constantly adding and removing entries (Maps are optimized for this).

  • You need to maintain the insertion order of your keys.

Use a Set when:

  • You need to store a list and ensure no duplicates exist.

  • Your primary operation is checking if an item exists (e.g., a list of "Liked" post IDs).

  • You want a high-performance way to handle mathematical operations like unions or intersections.

1 views