polygonplanet/weakmap-polyfill

Documenting limitations (frozen and sealed objects, values that outlive the WeakMap)

pygy opened this issue · 1 comments

pygy commented

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:

  1. The lib doesn't work with frozen and sealed objects.
  2. 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.

@pygy Thank you for the great issue! your advise is very important.
But, it will a possibility of affect other libraries by applying Object.getOwnPropertyNames or other monkeypatches, and I want to keep this library to a minimum implementation for only WeakMap, so I added limitations things in the document for now.