Skip to main content

Command Palette

Search for a command to run...

Understanding Map and Set in JavaScript

Published
3 min read
Understanding Map and Set in JavaScript

Before ES6, JavaScript developers mostly depended on Objects and Arrays to manage data. Objects were commonly used for storing key-value pairs, while Arrays handled lists of items. These structures worked well for basic applications, but as projects became larger and more dynamic, developers started facing limitations with performance, duplicate handling, and data organization.

To solve these issues, JavaScript introduced Map and Set in ES6. These modern data structures provide cleaner syntax, better performance in many cases, and more flexibility for handling complex data.

What is a Map in JavaScript?

A Map is a special collection that stores data in key-value format. Unlike regular Objects, a Map allows keys of any data type, including numbers, objects, and even functions.

In simple words, a Map is used to connect one value with another value using flexible keys.

const studentMarks = new Map();

const studentA = { id: 1 };
const studentB = { id: 2 };

studentMarks.set(studentA, 85);
studentMarks.set(studentB, 92);

console.log(studentMarks.get(studentA));
console.log(studentMarks.size);

Output:

85
2

Here, objects are being used directly as keys, which is not possible in a normal Object without converting them into strings.

Why Use a Map Instead of an Object?

One major advantage of Map is that it does not come with unwanted predefined keys like constructor or toString. It only stores the data you add manually.

Maps also preserve insertion order, making iteration predictable. Another benefit is performance. When applications frequently add or remove data, Maps are generally faster and more memory efficient.

Map vs Object

Feature Object Map
Key Types Strings & Symbols Any Data Type
Order Not guaranteed historically Maintains insertion order
Size Calculated manually .size property
Best Use Simple static data Dynamic data handling

What is Set?

A Set is a collection where duplicate values are automatically removed. Every item inside a Set must be unique.

const activeUsers = new Set();

activeUsers.add("Rahul");
activeUsers.add("Ankit");
activeUsers.add("Rahul");

console.log(activeUsers.size);
console.log(activeUsers.has("Ankit"));

Output:

2
true

Even though "Rahul" was added twice, the Set stored it only once.

Why Use Set Instead of Array?

Arrays allow duplicate values, which can create unnecessary complexity when uniqueness matters. Sets solve this problem automatically.

Searching inside a Set is also much faster than using methods like includes() on large Arrays.

Set vs Array

Feature Array Set
Duplicate Values Allowed Not Allowed
Access Method Index based Value based
Searching Speed Slower Faster
Main Purpose Ordered collections Unique collections

Real-World Use Cases

A Map is useful in applications like inventory systems, caching, or storing user session data, where keys may not always be strings.

A Set is perfect for tracking unique hashtags, online users, email subscribers, or filtering duplicate data from APIs.

Conclusion

Map and Set are modern JavaScript features designed to make data handling more efficient and readable. While Objects and Arrays are still important, using Map and Set in the right situations can improve performance, reduce bugs, and make your code much cleaner. For modern JavaScript development, understanding these structures is extremely valuable.