3 ms·
WeakMaps are useful for storing auxiliary information without having to pollute third-party objects and without impacting the ability to collect those objects.
by wycats 14y ago
WeakMaps are useful for storing auxiliary information without having to pollute third-party objects and without impacting the ability to collect those objects.
For example, jQuery stores information on a unique, generated key on DOM nodes. Ember does something similar for meta-information about an object. With WeakMaps, both could store the information in a WeakMap, and avoid polluting the object.
WeakMaps are easier to get into the language because the GC-related semantics are unobservable. If you have a key you can use in `get`, the key is by definition not collectible. For reasons involving security leaks, it is more tricky (but not impossible) to add features to the language that allow third-parties to observe when an object is collected.
That said, I am personally a strong advocate for Weak References in the language, and have been advocating their inclusion on TC39. Two open proposals are http://wiki.ecmascript.org/doku.php?id=strawman:weak_references http://wiki.ecmascript.org/doku.php?id=strawman:weak_referen... and http://wiki.ecmascript.org/doku.php?id=strawman:weak_refs http://wiki.ecmascript.org/doku.php?id=strawman:weak_refs. Active discussions involve post-mortem-finalization and avoiding security leaks.
As the maintainer of Ember and Ember Data, I fully agree with the need for this feature and will continue to work towards including it in the language.