垃圾收集缓存通过Javascript WeakMaps [英] Garbage-collected cache via Javascript WeakMaps

查看:207
本文介绍了垃圾收集缓存通过Javascript WeakMaps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JS中缓存大对象。这些对象通过键来检索,并且缓存它们是有意义的。但他们不会一下子适应记忆,所以我希望他们在需要的时候被垃圾收集 - 显然GC知道得更好。

制作这些东西很微不足道这样的缓存使用WeakReference或WeakValueDictionary在其他语言中找到,但在ES6中,我们有WeakMap,而 keys 很弱。

有可能使得像 WeakReference 或从 WeakMap

$ b $中创建垃圾收集缓存b

解决方案


是否可以从WeakMap制作WeakReference或从WeakMap制作垃圾收集缓存?

对于两个问题,答案都是否。


I want to cache large objects in JS. 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?

解决方案

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

AFAIK the answer is "no" to both questions.

这篇关于垃圾收集缓存通过Javascript WeakMaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆