WeakMap in ES6

ES6 introduced two weak type of data structures called WeakSet and WeakMap.

In this article we will learn about WeakMap.

WeakMap is a collection of key/value pair. The keys of WeakMap must be only object type while the value can be any JavaScript type.

Syntax

const weakMap = new WeakMap()

In WeakMap object references are held weakly. Whenever there are no references to the weak map keys outside a weak map, the key/value pair is automatically removed from the weak map.

Example:

let obj = {name: "John"}

const weakMap = new WeakMap()

weakMap.set(obj, "say hello")

console.log(weakMap.get(obj)) //  say hello

obj = null

console.log(weakMap.has(obj)) // false

In above example, the reference to the key object is no more available anywhere else except weak map. In this situation, the weakly held key/value pair will be removed from weak map.

Note: Only weak map key is weakly held, not value. If the references to the value is removed from outside of the weak map, it will not be garbage collected from weak map.

Example:

let obj1 = {name: "John"}

let val1 = "Hello ES6!"

let obj2 = {name: "Sara"}

let val2 = {age: 20}
 
const weakMap = new WeakMap()

weakMap.set(obj1, val1)

weakMap.set(obj2, val2)

obj1 = null

console.log(weakMap.has(obj1)) // false

val2 = null

console.log(weakMap.has(obj2)) // true

console.log(weakMap.get(obj2)) // {age: 20}

The above example has two elements in weak map. In the first element if the reference to the key object (obj1) is removed outside of the weak map, the key/value pair is removed from the weak map. For second element if the reference to the value (val2) is removed, it will not affect the weak map and it will still hold the value.

Initialize weak map

Weak map can be initialized same as Map by passing array containing arrays of two elements key and value to it’s constructor. If the key is non-object, it will throw an error.

Example:

let obj1 = {}

let obj2 = {}

const map = new WeakMap([[obj1, "Hello ES6!"], [obj2, "Hi there!"]])

Weak Map methods

WeakMap has no iteration methods like keys(), values() etc. The WeakMap can not be iterated using for…of or forEach.

Weak Map has four main methods:

set(key, value) – Adds new element in a weak map

get(key) – Returns specified element from a weak map

has(key) – Returns Boolean value according to the existence of a particular element

delete(key) – Removes particular element from a weak map

We will see all of the above methods in one example.

Example:

let obj1 = {}

const weakMap = new WeakMap()

weakMap.set(obj1, "new element")

console.log(weakMap.has(obj1)) // true

console.log(weakMap.get(obj1)) // new element

weakMap.delete(obj1)

console.log(weakMap.has(obj1)) // false

The above example shows all the methods of weak map. WeakMap does not have size property.

The differences between Map and WeakMap

  • In Map keys can be any JavaScript type while WeakMap keys are only object type.
  • When no more reference to the key of Map exist outside of a Map, the Map key/value pair will not affect but in WeakMap it will be garbage collected.
  • WeakMap does not have iterative methods like keys(), values() or for…of loop.
  • The length of the collection in weak map cannot determine as it does not support size property.

Happy Learning!

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments