javascript - Can we use WeakMap to improve garbage collection? - Stack Overflow

Say I have a class that does a lot of fetch requests.State of each request is stored in a property of

Say I have a class that does a lot of fetch requests. State of each request is stored in a property of that class, which now is an array of objects.

Objects in that array are not needed once the fetch req completes, so they are removed at that point. This is done by searching the array by some object property, then get the index, splice.

Then I saw WeakMap

At first glance, it looks like that array of objects can be replaced with a WeakMap. Then I would simply call map.delete(object) when fetch req completes, instead of searching an array.

However, I'm a bit confused about the values of WeakMaps. I my case, I don't need a value. I just need the object as a key. So I don't really understand the purpose of the value, or maybe WeakMap is the wrong tool for the job?

Say I have a class that does a lot of fetch requests. State of each request is stored in a property of that class, which now is an array of objects.

Objects in that array are not needed once the fetch req completes, so they are removed at that point. This is done by searching the array by some object property, then get the index, splice.

Then I saw WeakMap

At first glance, it looks like that array of objects can be replaced with a WeakMap. Then I would simply call map.delete(object) when fetch req completes, instead of searching an array.

However, I'm a bit confused about the values of WeakMaps. I my case, I don't need a value. I just need the object as a key. So I don't really understand the purpose of the value, or maybe WeakMap is the wrong tool for the job?

Share Improve this question asked Mar 13 at 11:57 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 2
  • 1 It sounds like you don't need the "weak" part. Actually, it sounds like you don't actually need this data structure at all. If you're not associating any values with the keys, and it doesn't sound like you want to perform containment tests, what good is this data structure even doing? – user2357112 Commented Mar 13 at 12:03
  • 2 For what ytou describe, you can use an ordinary Map. WeakMap is used when you have other references to the objects, but want them to be removed from the map automatically when all those other references become garbage. – Barmar Commented Mar 13 at 12:03
Add a comment  | 

1 Answer 1

Reset to default 1

Right off the bat, using an array sounds like the wrong choice. If you never need to iterate over these objects sequentially and always access an object by some property (assuming it's unique), you need a Map, not an array:

const cache = new Map();

function addToCache(fetchObj) {
    // O(1) operation!
    cache.set(fetchObj.someProperty, fetchObj);
}

function removeFromCache(fetchObj) {
    // O(1) operation!
    cache.delete(fetchObj.someProperty);
}

A WeakMap is essentially a Map, with an extra level of sophistication - it doesn't hold a strong reference to its keys. This mean that when the garbage collector runs, if a map's key is the only reference to that object, it will be removed. This also means that if the entry in the map was the only reference left to the value, it can also be removed.

Here, however, it looks like you know exactly when you'd want to remove an object from the data structure (be it an array as you're currently using or an array as you originally suggested). Since you're already managing the removal from the map, I don't see any benefit in using a WeakMap vs a straight up regual Map.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744702564a4588859.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信