3 ms·
> There's no such thing as a partial scope in JavaScript (to my knowledge, please correct if wrong). There's also no such thing as garbage collection in JavaSc
by munificent 2y ago
> There's no such thing as a partial scope in JavaScript (to my knowledge, please correct if wrong).
There's also no such thing as garbage collection in JavaScript. If you look purely at the language spec, then the language behaves as if there is infinite memory.
Garbage collection and how that interacts with scopes is purely an implementation detail. A conforming JavaScript implementation definitely could determine that a given local variable captured by a closure will no longer be accessed and free the associated memory. It's just that the implemented tested by the author (I'm assuming Chrome) doesn't do that.
- packetlost 2y ago> There's also no such thing as garbage collection in JavaScript. Incorrect. The details of garbage collection are left to the implementation, but the spec itself at least implies that GC is expected of implementations [0]. The current spec has an entire section on memory management [1] with several constructs for garbage collection and many explicit references to GC. That being said, it would be more correct to say this implementation of JavaScript does not perform capture analysis, effectively creating partial scopes. [0]: https://262.ecma-international.org/7.0/#sec-weakmap-objects https://262.ecma-international.org/7.0/#sec-weakmap-objects [1]: https://tc39.es/ecma262/#sec-managing-memory https://tc39.es/ecma262/#sec-managing-memory
- munificent 2y agoAh, weak references are a good point. Even so, you can have an entirely conforming JavaScript implementation with no GC. It just means that WeakRefs and WeakMaps will always continue to hold their references.