ES6 has a WeakMap

According to the description

In native WeakMap, references to key objects are stored "weakly", which means that they will not prevent garbage collection in the event that there are no other references to the object.

But how to get a list of values ​​from it? For example, I can match an object with a unique value (GUID). This is needed to free up resources by analogy with the finalizer in C #.

Or are there other methods to find out if the object has been garbage collected?

And how can equals be set? That is, you can keep a copy of an object with the same key field?

True found a link to the possibility of the finalizer https://www.npmjs.com/package/finalize

var finalize = require('finalize');  var obj = { x: 1337 }; finalize(obj, function () {   console.log(this.x); // this will print '1337' }); global.gc(); // nothing will happen, var obj above holds obj alive obj = null; global.gc(); // the previous line should trigger the callback above 

Inside this method uses https://github.com/TooTallNate/node-weak

 var weak = require('weak') // we are going to "monitor" this Object and invoke "cleanup" // before the object is garbage collected var obj = { a: true , foo: 'bar' } // Here's where we set up the weak reference var ref = weak(obj, function () { // `this` inside the callback is the EventEmitter. console.log('"obj" has been garbage collected!') }) // While `obj` is alive, `ref` proxies everything to it, so: ref.a === obj.a ref.foo === obj.foo // Clear out any references to the object, so that it will be GC'd at some point... obj = null // //// Time passes, and the garbage collector is run // // `callback()` above is called, and `ref` now acts like an empty object. typeof ref.foo === 'undefined' 

    2 answers 2

    https://learn.javascript.ru/set-map#weakmap--weakset

    WeakMap has a number of limitations:

    • No size property.
    • Cannot iterate through iterator or forEach.
    • There is no clear () method.

    Conclusion: it remains to use the has method to find out if an object exists or not.

    • And how can equals be set? That is, you can keep a copy of the object with the same field? - Serginio

    In the description it is clearly written that due to the implementation of this vandervafl, the search for keys and, therefore, values ​​is impossible.

    Because the links are weak, the WeakMap keys are not enumerable (that is, there is no method that returns a list of keys). Otherwise, the list would depend on the state of garbage collection, representing indeterminism. If you want to have a list of keys, you should maintain it yourself.

    No keys, no values.

    If you need brute force, use other constructions that are independent of the garbage collector. Same Map .

    • I just need to know whether the object has undergone garbage collection or not. Okay, I’ll look towards the finalizer npmjs.com/package/finalize - Serginio
    • You can implement tracking. But it is not necessary, for why? In JS, the collector is like the Gray Cardinal - he is, but he is not. - user207618
    • Well, as I understand it, you can force global.gc (); when a certain number of links are reached - Serginio