First, what I really want behind this question is to know if it would be possible to implement a reliable SoftMap in Javascript.
Babel, or the Memoizee lib seems to provide polyfills for ES6 WeakMap/Set. I've tried to read the code but I'm not really sure to understand how it works.
Coming from a Java background, I understand what is a WeakMap (we have WeakHashMap for a long time, and even Weak/Soft/Phantom references for a long time).
The thing is, in Java, to implement a WeakHashMap we use low-level primitives provided by the JVM like WeakReference.
I don't really understand how a polyfill of WeakMap
can be created without low-level reference primitives.
Can someone please explain to me, in an older browser we'd like to polyfill, how it would be possible to track for a map key, if some objects outside the map refers to that key?
Also something is unclear. In Java we have soft and weak references while in Javascript there's only weak map/set. After reading some stuff about WeakMap it is still not clear to me how the GC will decide to collect the map entry. If the key becomes unreachable, the GC:
- WILL always collect the entry immediately
- CAN always collect the entry immediately (but may do later if it does not require memory right now)
This is the distinction between Weak and Soft in Java, and it is not clear what behavior ES6 should implement.