Until ES5, the developers were building data structures with Arrays and Objects. Sometimes objects were used to make associative arrays, it’s something like a double duty for the Object data type. That’s why ES6 brings new data structures.
The new data structures in ES6 fall under 2 categories, they are
- Non-weak versions
var phoneBook = new Map();
Set is similar to an array but the items of the set must be unique.
var array = [1,2,3,1];
The last 2 new entrants are
The object is deallocated by setting to null
The object is no longer referenced
No keys, values, or entries of the Object exists. The entries in Map and Set count as references and the object is no longer garbage collected.
In the case of weakmap and weakset, the object references are held weakly.
In case of weakMap & weakSet, the objects are dereferenced when all other references are removed. Use these data structures when you are interested in better memory management.
In a weak map, a key is always an object.
The WeakMap is great for memos because once the object (used as the key) gets garbage collected, so does the computed value on the WeakMap.