How does Object.observe / unobserve interact with garbage collection?

Does the active Object.observe object on the object prevent garbage collection? Do you need to call Object.unobserve first so that it can be garbage collected? Or does a GCing object remove all active observers?

+6
source share
1 answer

Observing an object does not support it. Once an object dies, its observers simply will not receive any more events. At least that's true in the V8 implementation, which is the only one so far. It is probably safe to assume that it will be used for other implementations if this function becomes standard on some day.

However, the observation retains active active observers, as well as some internal, heaped data structures associated with each observer function. In fact, this additional data will only die when the function itself also died, even if it had long ceased to observe anything.

+6
source

Source: https://habr.com/ru/post/973831/


All Articles