Feature-Request: allow to iterate over WeakSet / WeakMap

Michael Kriegel michael.kriegel at actifsource.com
Wed Jul 13 12:13:11 UTC 2016

Hi everyone,

not sure, whether this is the right place for discussing a feature 
request - and how to find out, whether this was already 
proposed/discussed before and with which result... My google search did 
not bring up anything about it.

On my opinion it should be made possible to iterate over all elements in 
a WeakSet & WeakMap. I already found many cases, in which I'd like to 
have used the neat feature of "garbage collect the object, if it is not 
referred to by others anymore", but with the necessity to know, which 
objects are in the set (not only, if a given object is). An example:

Registering callbacks to objects:

class Foo {
   onBla(Handler) {

   someOtherMethod() {
     for (let X in this.BlaSuperWeakMap) {

   constructor() {
     this.BlaSuperWeakMap = new SuperWeakMap();

const MyFoo = new Foo();
   let MyHandler1 = function(){
     console.log('MyHandler1 called');
let MyHandler2 = function(){
   console.log('MyHandler2 called');
   console.log('MyHandler3 called');
MyHandler2 = undefined;

Expected result:
   MyHandler1 called
   MyHandler2 called

Basically the example is: Invoke the callback of all objects which still 
"exist" (in the meaning of having "non-weak" references towards them).

I know this may have some caveats. It would be necessary to check the 
reference count of the object in the SuperWeakSet, because the object 
may have no "non-weak" references left but still was not yet garbage 
collected. I do not know, whether current ECMAScript-Implementations and 
their garbage collecting solutions could handle this easily - basically 
it is the same decision, which the garbage collector would make: If an 
object can be disposed, because it has no non-Weak references pointing 
to it, the object is by definition not in the WeakSet anymore. And 
implementations could in that case even give a hint to the garbage 
collector, which may improve performance of applications, which use the 
SuperWeakSet alot.

Greetings, Michael

More information about the es-discuss mailing list