How to detect if an object has been garbage collected in JavaScript
It&#x27;s a shame that WeakMap and WeakSet aren&#x27;t enumerable. Ephemeron tables in Lua are enumerable, so creating a data structure that detects when an object is gone is possible. And because Lua supports finalizers, you can trigger an event the moment the object is garbage collected.[1][2]<p>[1] Lua also supports reviving objects. For example, if your finalizer leaks the object to the outside environment. Implementing this can be tricky, which is perhaps why JavaScript doesn&#x27;t permit it. So many limitations in JavaScript exist because a particular feature would be too difficult to implement in existing implementations.<p>[2] Of course, because Lua supports finalizers it&#x27;s possible to detect GC without using ephemeron tables. But if you can&#x27;t control the target object&#x27;s finalizer, then ephemeron tables are handy for doing this. And you can use your own finalizer on the ephemeron table value object to detect GC, though it&#x27;ll trigger one or two GC cycles behind when the key was collected.