Comments by "Kristopher Driver" (@paxdriver) on "An unexpected memory leak in JS" video.

  1. Imho there's an easier way to think about this so it's jot a headache: when globalThis has a property initialized that means any other functions defined next to it may use that scoped function, so it to needs wait to for the globalThis to garbage collect to be sure that the function gets collected and the array buffer with it. I will try this theory out on my own so this is sort of a bookmark for myself, but I would bet functions wait to garbage collect until after everything in that scope is collected first since functions are hoisted when defined they're probable last to get destroyed. That would mean defining functions in global scope or well defined scopes (unlike the example which initialized part of globalThis from a nested scope, effectively holding back garbage collection of the entire globalThis object). The practical way to avoid this is good practise already - don't initialize a global key from a nested function. A class instantiation of all possible keys would prevent this, I bet compiler frameworks prevent it, and even wasm or other transpilations would too, because they're not jit interpreters. It's probably the interpreter part that needs to hoist scopes like this because otherwise you'd just be setting an arbitrary depth for cutoff and that would be way worse performance than recursively hoisting an object being structurally modified in a deeper scope. Either by defining all functions in their useable widest scope like functional programming doctrine dictates, or by defining all objects with all possible placeholder keys like modular OOP doctrine suggests, I bet these paradigms eliminate this memory leak by design, or at worst limit it to an interpreted, unoptimized runtime anyway where that kind of leakage should be expected. Love these videos btw, thanks a mil. Can't wait to try this out with iffe and async and every variety I can imagine lol. I bet the browser throwing timeout into its own thread loop has something to do with it too, or specifically array buffers working at lower level than standard objects due to their memory allocation benefits maybe plays a role? There are so many different reason I can imagine this made sense to ECMA lol
    1