WeakMaps are similar to maps, however they only hold a weak copy of the items, javascript will handle the garbage collection for you.
Code
//ES6 Maps and Sets - The WeakMap
let cardAce = {
name : 'Ace of Spades'
};
let cardKing = {
name : 'King of Clubs'
};
// In WeakMap's your key has to be a javascript object
// weakmaps hold 'weak' references meaning it is garbage collected
// therefore keys must be referencesTypes
// weakmaps identify which objects arent being used anymore and removes them from memory
// looping is not possible, but you can get an object with a key
let keyOne = {a:1};
let keyTwo = {b:2};
let deck = new WeakMap();
deck.set(keyOne, cardAce);
deck.set(keyTwo, cardKing);
// Get object from weakmap by key
console.log(deck.get(keyOne))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30