javascriptcachinggarbage-collectionecmascript-6ecmascript-harmony

Garbage-collected cache via Javascript WeakMaps


I want to cache large objects in JavaScript. These objects are retrieved by key, and it makes sense to cache them. But they won't fit in memory all at once, so I want them to be garbage collected if needed - the GC obviously knows better.

It is pretty trivial to make such a cache using WeakReference or WeakValueDictionary found in other languages, but in ES6 we have WeakMap instead, where keys are weak.

So, is it possible to make something like a WeakReference or make garbage-collected caches from WeakMap?


Solution

  • is it possible to make WeakReference from WeakMap or make garbage-collected cache from WeakMap ?

    AFAIK the answer is "no" to both questions.