JavaScript WeakMap and WeakSet

JavaScript WeakMap and WeakSet

JavaScript WeakMap and WeakSet

Introduction

WeakMap and WeakSet are similar to Map and Set, but they have key differences that make them useful in specific situations.

Key Characteristics of WeakMap and WeakSet

  1. Keys in WeakMap and values in WeakSet must be objects (not primitives like numbers, strings, or booleans).
  2. Garbage collection-friendly: If an object key/value is no longer referenced elsewhere, it is automatically removed from the WeakMap or WeakSet.
  3. No size property or iteration methods: Unlike Map and Set, they cannot be iterated over or checked for size.

JavaScript WeakSet

A WeakSet is similar to a Set, but it only stores objects and automatically removes them when they are no longer referenced elsewhere.

Creating a WeakSet

let weakSet = new WeakSet();

Adding Objects to a WeakSet

let user1 = { name: "Alice" }; let user2 = { name: "Bob" }; weakSet.add(user1); weakSet.add(user2); console.log(weakSet); // We can't directly see WeakSet contents

Checking if an Object Exists

console.log(weakSet.has(user1)); // true console.log(weakSet.has({ name: "Alice" })); // false (different object)

Removing an Object

weakSet.delete(user1); console.log(weakSet.has(user1)); // false

Automatic Garbage Collection

If user2 is set to null and is not referenced anywhere else, it will be removed from weakSet automatically.

user2 = null; // Now it's removed from WeakSet automatically

🔸 No way to check size:
Unlike Set, WeakSet does not have .size because its elements can be garbage collected at any time.

JavaScript WeakMap

A WeakMap is similar to a Map, but keys must be objects, and it allows automatic garbage collection.

Creating a WeakMap

let weakMap = new WeakMap();

Adding Key-Value Pairs

let person = { name: "John" }; weakMap.set(person, "Developer"); console.log(weakMap.get(person)); // Output: Developer

Checking If a Key Exists

console.log(weakMap.has(person)); // true

Removing a Key

weakMap.delete(person); console.log(weakMap.has(person)); // false

Automatic Garbage Collection

If the person object is set to null, it is automatically removed from the WeakMap.

person = null; // Now it's removed from WeakMap automatically

🔸 No .size, .keys(), or .values()
WeakMaps do not support iteration or size checking because their keys can be garbage collected at any time.

Differences Between WeakMap and Map

FeatureMapWeakMap
KeysAny type (objects, strings, numbers, etc.)Objects only
Garbage CollectionNo automatic removalRemoves entries when keys are no longer referenced
IterationYes (.keys(), .values(), .entries())No iteration methods
.size PropertyYesNo

Differences Between WeakSet and Set

FeatureSetWeakSet
ValuesAny typeObjects only
Garbage CollectionNo automatic removalAutomatically removes unreferenced objects
IterationYes (forEach(), for...of)No iteration methods
.size PropertyYesNo

When to Use WeakMap and WeakSet?

WeakMap:

  • Storing metadata about objects (caching, tracking elements in the DOM)
  • Keeping data private (since WeakMap keys cannot be accessed directly)

WeakSet:

  • Keeping track of unique objects without preventing garbage collection

Summary

WeakMap and WeakSet store only objects
✔ They automatically remove unreferenced objects
✔ They do not have iteration methods or .size
WeakMap is useful for caching and private data storage
WeakSet is useful for tracking unique objects

Now you know how to use WeakMap and WeakSet in JavaScript! Let me know if you need more examples. 

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close