m0meni
m0meni

Reputation: 16445

Garbage collection on Map and WeakMap collections in es6

I was reading the WeakMap's description and it said:

In native WeakMaps, references to key objects are held "weakly", which means that they do not prevent garbage collection in case there would be no other reference to the object.

From reading this, my understanding is that WeakMaps are used in instances where you're trying to take advantage of JavaScript's garbage collection. What I don't understand fundamentally is the line I bolded.

Why, in a normal Map, would objects with no references to them not be garbage collected? What's the advantage to this, and do I have to explicitly delete from normal Maps?

Upvotes: 6

Views: 2985

Answers (2)

m0meni
m0meni

Reputation: 16445

"Why, in a normal Map, would objects with no references to them not be garbage collected?" because the map itself has a reference to the object. As long as there is a reference, the object is not gced. - Felix Kling

Upvotes: 2

amara
amara

Reputation: 2286

in a normal Map, the Map is a reference to the object

Upvotes: 1

Related Questions