Thursday, July 27, 2017

WeakMap

A WeakMap is distinguishable from a map in both JavaScript and TypeScript by way of the fact that it is explicitly instantiated as a WeakMap, perhaps like so:

var weakSause = WeakMap();

 
 

The line items in a WeakMap are vulnerable to being cleaned up by garbage collection if they are unused and this is both the strength and the weakness of a WeakMap and also the distinction between a Set and a WeakSet as well. (The sets let you store a collection of things as long as each thing is unique.) Why would you want to do this? The reason to go there is if you have a dictionary that can grow fat overtime due to you pumping a bunch of gunk into it and never cleaning it up. What if you cause a memory leak?

No comments:

Post a Comment