Map vs WeakMap Garbage Collection in JavaScript

JavaScript provides both Map and WeakMap to store key-value pairs, but the critical difference between them lies in how they handle memory management and garbage collection. A standard Map maintains strong references to its keys, meaning objects used as keys will not be cleared from memory even if all other references to them are removed. In contrast, a WeakMap holds “weak” references to its keys, allowing the JavaScript garbage collector to free up memory automatically as soon as an object key loses all other references in the application.

Strong References in Map

In a standard Map, keys can be of any data type, including primitives, functions, and objects. When an object is used as a key in a Map, the map retains a strong reference to that object.

As long as the Map instance itself remains in memory, the key object and its associated value will also remain in memory. Even if you set the original variable pointing to the object to null, the garbage collector will not reclaim the object because the Map is still referencing it.

let user = { name: "Alice" };
const userMap = new Map();

userMap.set(user, "User profile data");

// Remove the original reference
user = null;

// The object { name: "Alice" } remains in memory because userMap holds a strong reference to it.

To release the memory in a Map, you must explicitly remove the entry using map.delete(key) or clear the entire map with map.clear(). Failing to do so can lead to memory leaks, especially in long-running applications or single-page applications.

Weak References in WeakMap

A WeakMap addresses this memory retention issue by holding only weak references to its keys. Because of how weak references work, WeakMap has specific constraints:

  1. Keys must be objects: Primitive values (like strings, numbers, or booleans) cannot be used as keys in a WeakMap because primitives cannot be garbage-collected.
  2. Keys are weakly held: If no other reference to a key object exists outside the WeakMap, the garbage collector will automatically reclaim the object and remove the corresponding key-value pair from memory.
let user = { name: "Alice" };
const userWeakMap = new WeakMap();

userWeakMap.set(user, "User profile data");

// Remove the original reference
user = null;

// The object { name: "Alice" } and its associated value will be automatically garbage-collected.

Why WeakMap Cannot Be Iterated

Because garbage collection runs non-deterministically at the JavaScript engine’s discretion, an entry in a WeakMap could be removed at any moment. As a result, WeakMap does not support:

These limitations ensure the integrity of the data structure, preventing unpredictable behavior caused by the timing of the garbage collector.

Summary of Differences

Feature Map WeakMap
Garbage Collection Prevents GC of key objects (strong reference). Allows GC of key objects when unreferenced elsewhere (weak reference).
Key Types Any type (objects, primitives, functions). Objects only (and non-registered symbols in modern engines).
Memory Leak Risk High if entries are not manually deleted. Low, memory cleans up automatically.
Iterability Fully iterable (keys(), values(), forEach). Not iterable.
Size Property Yes (map.size). No.

When to Use Each