Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Ecma 1
  • Overview
  • Transcript

4.3 Weak Maps and Weak Sets

In this lesson, you'll learn about WeakSet and WeakMap. These are similar to the set and map data structures, but with contains weakly referenced keys and values. In this lesson, I'll explain how the weak references don't prevent the target values from being garbage collected.

4.3 Weak Maps and Weak Sets

Hi, folks. In this lesson, we're gonna take a look at some objects that are extremely similar to the sets and maps that we've already looked at, WeakSets and WeakMaps. The difference between these and the regular sets of maps, is that object references within them are held weakly, and so will not impede garbage collection. Let's take a look at week sets first. We saw earlier in the course that sets can be composed of any type of data. This is one place where week sets differ from sets. Week sets can only be composed of objects. To create a week set we use the week set constructor as with a regular set, we can parse an iterable object and the elements of this object will be added to the set. Each value in a weak set is still unique. Weak sets have a much smaller API than sets. We have only the add delete and has methods. With the add method, we can pass the objects that we'd like to add to the sets. With the has method, we pass reference to the object that we'd like to see whether the weeks that contains it. To remove an item from a week set, we use the delete method With the delete method, we also pass a reference to the item that we'd like to remove. The main difference between sets and weak sets is that the objects held in a weak set are held weekly. If no other references to an object that a weak set contains exist, the object can be safely garbage collected to reduce the memory footprint of your code. Another difference is that weak sets are not enumerable, let's move on to weak maps. As we saw earlier in this section, the keys of map objects can be of any data type. That's not the case with a weak map. Similar to a weak set, the keys of a weak map must be objects. Just like with maps, it creates a wig map using a constructor. In this case the constructor is weak map instead of map. Just like with the set or map constructors, we can also parse an iterable object to the constructor for a new WeakMap to be populated. Also just like with a weak set, the API of the WeakMap compared to a regular map is much diminished. With WeakMap, we only have the methods, delete get, has and set, which also means that WeakMaps like WeakSets can't be iterated. What separates WeakMaps from maps is that references to the keys in a WeakMap are held weekly, which means that both, the key and it's associated value can be garbage collected If no other references to the key exist. So, in this lesson we looked at WeakSets and WeakMaps, which is similar to their set and map counterparts except that the references that each holds to its values or keys, respectively, are weak and do not prevent garbage collection when no other references to them exist. We saw that they both have a small, focused API for adding and getting values or determining whether a value or key exists. But then neither of these types of data structures can be iterated. Thanks for watching.

Back to the top