Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is WeakMap clear() method deprecated?

I have been working with WeakMaps in JavaScript, and after checking the documentation I realized that the clear method has been deprecated / removed from ECMAScript 6.

What is the reason for this? Why force us to do a clear function like:

clear() {
  this._weakmap = new WeakMap()
}
like image 764
Flame_Phoenix Avatar asked May 30 '16 14:05

Flame_Phoenix


People also ask

Which is not a valid method for Javascript WeakMap?

WeakMap does not support iteration and methods keys() , values() , entries() , so there's no way to get all keys or values from it.

Why are WeakMap keys not enumerable?

A WeakMap can be a particularly useful construct when mapping keys to information about the key that is valuable only if the key has not been garbage collected. But because a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable. There is no method to obtain a list of the keys.

What is the difference between set map and WeakSet WeakMap?

In comparison to a Map , a WeakMap is very much the same but the references it holds are weak references, meaning it won't prevent garbage collection from removing values it references if they are not strongly referenced elsewhere.


2 Answers

“The mapping from weakmap/key pair value can only be observed or affected by someone who has both the weakmap and the key. With clear(), someone with only the WeakMap would’ve been able to affect the WeakMap-and-key-to-value mapping.”

Mark Miller

The reason for this restriction are security concerns:

A key property of Weak Maps is the inability to enumerate their keys. This is necessary to prevent attackers observing the internal behavior of other systems in the environment which share weakly-mapped objects. Should the number or names of items in the collection be discoverable from the API, even if the values aren't, WeakMap instances might create a side channel where one was previously not available.

tc39wiki

A enumerable WeakMap could possibly also affect GC, since you could then observe the GC process indirectly. Thus, to ensure a predictable design clear was removed as well.

like image 54
Iven Marquardt Avatar answered Oct 20 '22 01:10

Iven Marquardt


It is deprecated because it prevents inverted implementation of WeakMap.

See Removal of WeakMap/WeakSet clear.

If WeakMaps/WeakSets are not inspectable (via iteration) and do not have a clear operation, then the inverted implementation technique can be use used. This technique eliminates significant GS complexity.

Inverted implementation description, from the same source:

design for an inverted implementation:

Every object internally maintains a table (probably a hash table if it contains more than a few elements) that is used to implement WeakMap/Set. Entry in the table is a key/value pair where the key is a WeakMap/Set instance. Values are arbitrary ES values. Let's call such a table an "inverted map" and generically refer to such WeakMaps/Sets as WCs.

like image 35
Suma Avatar answered Oct 20 '22 00:10

Suma