JavaScript

It’s 2023. Start using JavaScript Maps and Sets

As a JavaScript developer, you are probably familiar with the two most commonly used data structures, Arrays and Objects. They are both…

Fotis Adamakis
Fotis Adamakis
Senior Software Engineer / Technical Writer
3 min read
May 8, 2023

It’s 2023. Start using JavaScript Maps and Sets

As a JavaScript developer, you are probably familiar with the two most commonly used data structures, Arrays and Objects. They are both extremely versatile and powerful however, two more options are often overlooked: Maps and Sets. Let’s explore how they can make your code more efficient and easier to read.

Maps

Maps are similar to Objects in that they store key-value pairs. However, unlike Objects, Maps allow any type of value to be used as a key, including objects and functions. This makes them much more flexible than Objects, especially when you need to store complex data structures.

To create a Map, you can use the Map constructor:

const myMap = new Map();

You can also initialize a Map with an array of key-value pairs:

const myMap = new Map([
  ["key1", "value1"],
  ["key2", "value2"],
]);

To add a key-value pair to a Map, you can use the set() method:

myMap.set("key3", "value3");

To retrieve a value from a Map, you can use the get() method:

const value = myMap.get("key3");

Maps also have other useful methods, such as [has()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) to check if a key exists in the Map, delete() to remove a key-value pair, and clear() to remove all key-value pairs and, of course, the very useful .size which is not available in objects.

[Sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)

Sets are similar to Arrays in that they store a collection of values. However, unlike Arrays, Sets only allow unique values. This makes them useful when you need to ensure that a collection of values contains no duplicates.

To create a Set, you can use the Set constructor:

const mySet = new Set();

You can also initialize a Set with an array of values:

const mySet = new Set(["value1", "value2"]);

To add a value to a Set, you can use the add() method:

mySet.add("value3");

To check if a value exists in a Set, you can use the has() method:

const exists = mySet.has("value3");

Sets also have other useful methods, such as delete() to remove a value and clear() to remove all values.

Why use Maps and Sets?

Now that we know how to use Maps and Sets, let’s talk about why you should start using them in your JavaScript code.

  1. Performance: Maps and Sets can be faster than Arrays and Objects for certain operations. For example, checking if a key exists in a Map using the has() method is faster than checking if a key exists in an Object using the in operator.
  2. Flexibility: Maps allow any type of value to be used as a key, which can be useful when you need to store complex data structures. Sets only allow unique values, which can be useful when you need to ensure that a collection of values contains no duplicates.
  3. Readability: Using Maps and Sets can make your code more readable by clearly communicating your intentions. For example, if you are using a Set to store a collection of unique values, it is immediately clear to anyone reading your code that duplicates are prohibited.

Conclusion

Maps and Sets are powerful data structures that can make your JavaScript code more efficient and easier to read. They offer flexibility and performance benefits that Arrays and Objects can’t match. So, be cool and start using Maps and Sets in your JavaScript code today!

It’s_2023. Start using JavaScript Maps and Sets

Fotis Adamakis

Fotis Adamakis

Senior Software Engineer / Technical Writer

Experienced software engineer writing about front end architecture, accessibility, system design, and developer productivity. Lessons from building and maintaining large-scale frontend applications, with a focus on practical patterns that make codebases easier to understand, scale, and evolve.

Barcelona, Spain