The approach taken by this lib is pretty good, since the GC lifetime of values is tied to that of keys, however, it has a few limitations that may be worth documenting:
- The lib doesn't work with frozen and sealed objects.
- The values held by a WeakMap can't be collected once the map itself is GCed, since the values here are tied to the keys.
A third thing, which is more of a suggestion:
Even if they are not enumerable, the keys can be obtained in some IE versions by using Object.getOwnPropertyNames. This could be mitigated relatively cheaply by storing the mapping inside a single object that is tied to the key object using a secret field as a value. You can then monkeypatch Object.getOwnPropertyNames to strip that secret field from the results and, also monkeypatch Object.getOwnPropertyNames if present (in IE thanks to a polyfill, not sure about other envs).
If this monkeypatch approach seems workable to you, you could also solve 1) by monkeypatching Object.freeze and Object.seal to add the secret field before freezing.
The approach taken by this lib is pretty good, since the GC lifetime of values is tied to that of keys, however, it has a few limitations that may be worth documenting:
A third thing, which is more of a suggestion:
Even if they are not enumerable, the keys can be obtained in some IE versions by using
Object.getOwnPropertyNames. This could be mitigated relatively cheaply by storing the mapping inside a single object that is tied to thekeyobject using a secret field as a value. You can then monkeypatchObject.getOwnPropertyNamesto strip that secret field from the results and, also monkeypatchObject.getOwnPropertyNamesif present (in IE thanks to a polyfill, not sure about other envs).If this monkeypatch approach seems workable to you, you could also solve 1) by monkeypatching
Object.freezeandObject.sealto add the secret field before freezing.