JavaScript WeakMap And WeakSet

In this tutorial, we will learn about JavaScript WeakMap and WeakSet objects. WeakMap and WeakSet are two new data structures introduced to JavaScript in ES6. These data structures provide a way to store data in a memory-efficient way and are useful for working with large datasets. We will discuss their features, use cases, and how to use them in your code.

Why should you use it?

  • Memory efficient - WeakMap and WeakSet use less memory than other data structures.
  • Optimized for large datasets - WeakMap and WeakSet are optimized for working with large datasets.
  • Easy to use - WeakMap and WeakSet are easy to use and understand.

WeakMap

The WeakMap object is a collection of key/value pairs in which the keys are objects and the values can be arbitrary values. It is similar to Map, but it only allows objects as keys and the keys are held weakly. This means that if there is no other reference to the key object, the entry in the WeakMap can be garbage collected. WeakMap is a perfect fit for private properties in JavaScript.

WeakMap objects are collections of key/value pairs in which the keys are objects and the values can be arbitrary values. The key difference between WeakMap and Map is that the keys in WeakMap are held weakly. This means that if there is no other reference to the key object, the entry in the WeakMap can be garbage collected.

WeakMap objects have some advantages over Map objects. For example, they are not enumerable, so you can’t use the for...in loop to iterate over the values in a WeakMap. This makes them useful for storing private data, as they can’t be enumerated or accessed by other code.

WeakMap objects also have the advantage of being able to store weak references to objects. This means that if there is no other reference to the object, the entry in the WeakMap can be garbage collected. This makes WeakMap objects ideal for storing private data, as the data will be automatically cleaned up when there are no other references to it.

Creating a WeakMap

To create a WeakMap, you can use the new keyword. To add items to the WeakMap, you can use the set() method, which takes two arguments: the key and the value.

script.js
let weakMap = new WeakMap();

let key = {};
let value = 'value';

weakMap.set(key, value);

You can also use the get() method to retrieve the value associated with a given key.

script.js
let weakMap = new WeakMap();

let key = {};
let value = 'value';

weakMap.set(key, value);

let retrievedValue = weakMap.get(key);

console.log(retrievedValue); // 'value'

WeakMap vs Map

The main difference between WeakMap and Map is that the keys in WeakMap are held weakly. This means that if there is no other reference to the key object, the entry in the WeakMap can be garbage collected. This makes WeakMap objects ideal for storing private data, as the data will be automatically cleaned up when there are no other references to it.

Another difference between WeakMap and Map is that WeakMap objects are not enumerable. This means that you can’t use the for...in loop to iterate over the values in a WeakMap. This makes them useful for storing private data, as they can’t be enumerated or accessed by other code.

Finally, WeakMap objects are not iterable, so you can’t use the for...of loop to iterate over the values in a WeakMap. This makes them useful for storing private data, as they can’t be iterated or accessed by other code.

To learn more about weakmap, visit .

WeakSet

A WeakSet is a special type of collection that allows you to store weakly held objects. This means that the objects stored in a WeakSet can be garbage collected if there are no other references to them. WeakSets are useful for keeping track of objects that need to be removed from memory when they are no longer needed.

A WeakSet is similar to a Set, with the main difference being that it only stores objects, not primitive values. Also, unlike a Set, a WeakSet does not have a size property, and its entries cannot be iterated over. This makes WeakSets more memory-efficient than Sets.

WeakSets are useful for keeping track of objects that need to be removed from memory when they are no longer needed. For example, a WeakSet could be used to keep track of objects that have been marked as "garbage" and should be removed when no longer needed. WeakSets are also useful for keeping track of objects that are in a "temporary" state, such as objects that have been created for a short period of time and no longer need to be kept in memory.

To create a WeakSet, you can use the WeakSet constructor, which takes an iterable (an array or other iterable object) of objects as its argument. The WeakSet constructor will add each object in the iterable to the WeakSet.

script.js
let myWeakSet = new WeakSet([{a:1}, {b:2}, {c:3}]);

Once a WeakSet is created, you can add objects to it using the add() method. This method takes an object as its argument and adds it to the WeakSet.

script.js
let myWeakSet = new WeakSet([{a:1}, {b:2}, {c:3}]);

let obj = {d:4};
myWeakSet.add(obj);

console.log(myWeakSet);

You can also remove objects from a WeakSet using the delete() method. This method takes an object as its argument and removes it from the WeakSet.

script.js
let myWeakSet = new WeakSet([{a:1}, {b:2}, {c:3}]);

let obj = {b:2};
myWeakSet.delete(obj);

console.log(myWeakSet);

Finally, you can check if an object is in a WeakSet using the has() method. This method takes an object as its argument and returns true if the object is in the WeakSet, or false if it is not.

script.js
let myWeakSet = new WeakSet([{a:1}, {b:2}, {c:3}]);

let obj = {b:2};
console.log(myWeakSet.has(obj));

To learn more about weakset, visit .