Maps and Sets
T.J. Crowder · 2020
This chapter deals with ES2015+'s Maps, Sets, WeakMaps, and WeakSets. Maps store key/value pairs, where the key and value can be (almost) anything. Sets store unique values. WeakMaps are similar to Maps, but the keys are objects and those objects are held weakly (they can be garbage collected, which removes the entry from the map). WeakMaps let one store a value related to an object (the key) without forcing the key to remain inmemory; the key is weakly held by the map. WeakMaps are not iterable and provide very little information about their contents. WeakSets hold unique objects weakly. WeakSets are the Set equivalent of WeakMaps: sets of objects where being in the set does not prevent cleanup of the object. One can think of a WeakSet as a WeakMap where the key is the value in the set and the value is (conceptually) true.