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'