It seems this is a result of misunderstanding on part of the polyfill authors, please consider writing a bug report.
These approaches indeed keep a strong reference to the object, an implementation which is technically conforming, because the program can't argue that a garbage collector is broken just because it was never observed removing an object. However, to achieve this feat would have been much easier: just store the object in a property and don't bother with a WeakMap
at all.
I think some people naïvely expect that a WeakMap
has weak references to the values, as you observed. I also used to have such misconception before reading the docs carefully. Nevertheless, this would not explain why WeakMap
has been in the spec since 2015 while WeakRef
hasn't made it past the proposal stage yet, for over 3 years. Such naïve collection would be like a Map
of WeakRefs
, so why would the language not also expose a single one?
Truth is, it is impossible to mimic WeakRef
with WeakMap
and careful design has been taken to make it so (primarily, that it is not enumerable). Why? While it is perfectly possible to implement this behaviour reliably, the sentiment in the design group is towards determinism and predictability. This short paragraph summarizes it nicely, with reasoning and an example considered a wrong design past decision. The following two sentences directly preclude anything like a WeakRef
:
This means that you shouldn’t expose any API that acts as a weak reference, e.g. with a property that becomes null once garbage collection runs. Object and data lifetimes in JavaScript code should be predictable.
With this in mind, in an implementation following the W3C TAG guidelines, not only WeakMap
can't be used, but not even any other approach (which should answer your last question in the comments), except possibly using an ugly and unreliable loophole like that mentioned in the example, for narrowly specific cases. Unless the opinion of the committee changes radically, the WeakRef
proposal may never really be standardized. Let's hope that a carefully worded note of caution, as included in the current version, and an appeal to excess memory usage and other interesting and well-founded use cases, will eventually allow such exception.