Blog / October 9, 2023 / 5 mins read / By Suneet Agrawal

Map vs WeakMap in JavaScript

JavaScript provides various data structures to manage collections of key-value pairs, and two commonly used ones are Map and WeakMap. Both are used to associate values with keys, but they have distinct characteristics and use cases. In this blog, we will explore the differences between Map and WeakMap with examples to help you understand when to use each one.

Map

A Map is a built-in JavaScript data structure introduced in ECMAScript 6 (ES6) that allows you to store key-value pairs where the keys can be of any type, including objects and primitive types. Map is especially useful when you need to maintain a mapping of keys to values, and it preserves the order of key insertion. Here’s an example of how to create and use a Map:

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

// Adding key-value pairs to the Map
const key1 = 'name';
const value1 = 'Alice';
myMap.set(key1, value1);

const key2 = { id: 1 };
const value2 = 'Bob';
myMap.set(key2, value2);

// Getting values from the Map
console.log(myMap.get(key1)); // Output: 'Alice'
console.log(myMap.get(key2)); // Output: 'Bob'

// Checking if a key exists in the Map
console.log(myMap.has(key1)); // Output: true

// Deleting a key-value pair from the Map
myMap.delete(key1);

// Iterating through the Map
for (const [key, value] of myMap) {
  console.log(key, value);
}

WeakMap

A WeakMap is similar to a Map in that it stores key-value pairs, but it has a crucial difference: it can only use objects as keys and does not prevent those objects from being garbage collected when they are no longer in use. This behavior makes WeakMap suitable for scenarios where you want to associate metadata with objects or manage relationships without preventing them from being automatically cleaned up by the garbage collector. Here’s an example:

// Creating a WeakMap
const myWeakMap = new WeakMap();

// Creating objects to use as keys
const key1 = { id: 1 };
const key2 = { id: 2 };

// Adding key-value pairs to the WeakMap
myWeakMap.set(key1, 'Alice');
myWeakMap.set(key2, 'Bob');

// Getting values from the WeakMap
console.log(myWeakMap.get(key1)); // Output: 'Alice'
console.log(myWeakMap.get(key2)); // Output: 'Bob'

// Checking if a key exists in the WeakMap
console.log(myWeakMap.has(key1)); // Output: true

// Deleting a key-value pair from the WeakMap
myWeakMap.delete(key1);

// The WeakMap automatically removes references to deleted keys
console.log(myWeakMap.has(key1)); // Output: false

Differences Between Map and WeakMap

Now that we’ve discussed Map and WeakMap individually, let’s highlight the key differences between them:

  1. Key Types:
  • Map: Map can use any type of value as a key, including primitive types (numbers, strings, booleans), objects, and even functions.
  • WeakMap: WeakMap can only use objects as keys. Attempting to use non-object values as keys will result in an error. This restriction is because WeakMap is designed to work with object references.
  1. Garbage Collection:
  • Map: Objects used as keys in a Map are held by strong references, meaning they won’t be garbage collected as long as the Map retains a reference to them. This can lead to memory leaks if you unintentionally retain objects.
  • WeakMap: Objects used as keys in a WeakMap are held by weak references, allowing them to be garbage collected as soon as no other strong references to those objects exist. This behavior is useful for scenarios where you want to avoid memory leaks and allow automatic cleanup of objects.
  1. Iteration:
  • Map: Map allows easy iteration through key-value pairs in a predictable order, maintaining the insertion order of keys.
  • WeakMap: WeakMap does not provide direct iteration methods, and you typically use it for value retrieval rather than iteration. It does not guarantee any specific order of key-value pairs.
  1. Size Property:
  • Map: Map has a size property that allows you to determine the number of key-value pairs it contains.
  • WeakMap: WeakMap does not have a size property, so you cannot directly obtain the number of key-value pairs it contains.

Use Cases

Now, let’s explore the common use cases for both Map and WeakMap to help you understand when to use each one:

Use Map When:
  • Maintaining Key-Value Pairs: You need to maintain a collection of key-value pairs, and the keys can be of diverse types, including both primitive values and objects.
  • Iteration and Predictable Order: You require easy iteration through the key-value pairs and want to ensure that the order of insertion is preserved.
  • Strong References: You want to ensure that the objects used as keys are not garbage collected as long as they are associated with the Map.
  • Diverse Key Types: You need to use various types of values as keys, including non-object types like strings and numbers.
Use WeakMap When:
  • Associating Metadata with Objects: You want to associate additional information or metadata with objects but don’t want to prevent those objects from being garbage collected when they are no longer in use.
  • Object Relationships: You need to create relationships between objects, and you want these relationships to be automatically cleaned up when the objects are no longer referenced by other parts of your code.
  • Avoiding Memory Leaks: You’re concerned about potential memory leaks when working with long-lived objects and want to ensure that the garbage collector can reclaim memory efficiently.
  • Exclusively for Objects: You need to use objects as keys, and you’re not interested in associating non-object values with keys.

Conclusion

In this blog, we’ve explored the differences between Map and WeakMap in JavaScript, two essential data structures for managing key-value pairs. Understanding the distinctions between these structures is crucial for making informed decisions when working with collections of data in your JavaScript applications.

Map is versatile and suitable for scenarios where you need to maintain key-value pairs, regardless of the types of keys, while preserving insertion order. It’s perfect for situations where you want to maintain strong references to keys.

WeakMap, on the other hand, is specialized for use with objects as keys. It allows you to associate metadata with objects and create object relationships without preventing those objects from being garbage collected. This is particularly valuable for avoiding memory leaks and ensuring efficient memory management.

When choosing between Map and WeakMap, consider the specific use case and requirements of your application. By doing so, you can harness the power of these data structures to build more efficient and robust JavaScript applications. Understanding their key differences and appropriate use cases will enable you to make informed decisions and write cleaner, more efficient code.

Comments