# WeakMap

* References to keys/values held weakly
* Do not prevent garbage collection
* Small api ( add, delete, check for values ) compared to set/map
* cann't be iterated
* Have no size as it does garbage collected&#x20;

**Note**

* keys must be objects&#x20;
* the values can be arbitrary values.

```javascript
var wm = new WeakMap();
wm.add()
wm.delete()
wm.has()
```

## WeakMap Use Case

```javascript
let dog1 = {name: 'Tomy'};
let dog2 = {name: 'Tuffy'};

const strongMap = new Map();
const weakMap = new WeakMap();

strongMap.set(dog1, 'Tomy is the best');
weakMap.set(dog2, 'Tuffy is the best');

dog1 = null;
dog2 = null;

// strongMap.size => 1
// WeakMap.size => undefined
strongMap -> Map(1) {Object {name: "Tomy"} => "Tomy is the best"}
weakMap -> WeakMap {} // expected, as it garbage collected item in it, because dog1 reference is null now
```

## WeakMap Demo

```javascript
var wm = new WeakMap();
wm.set({name: 'hemant'}, 30).set({age: 20}, function(){ return 'your age'; })
console.log(wm); // WeakMap {Object {name: "hemant"} => 30, Object {age: 20} => function}
```
