4.3 Weak Maps and Weak Sets
In this lesson, you'll learn about
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.
1.Introduction2 lessons, 09:45
2.ES6 Basics3 lessons, 23:18
3.Built-in Objects4 lessons, 22:32
4.Data Structures5 lessons, 21:02
5.New Function Types2 lessons, 12:05
6.More New Syntax3 lessons, 23:01
7.Conclusion1 lesson, 00:46
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.