Good question! I *believe* that the garbage collector will clean up anything (on some schedule) that does not have a pointer to it in scope. So a purely inline "return [...arrayToCopy, lastItem]" will stick around as long as you have a variable pointing to it. This now has me wondering about closures - perhaps its possible to have a reference to a closure that has an internal variable that's never used again, but the garbage collector doesn't know that. I'm not 100% sure about these things, so I hope someone else can chime in with a more complete answer!
Good video 👍
Thanks! Glad you enjoyed :-)
since the destructuring (...) creates a temp location in memory, how does the garbage collection works, do we end up leaking memory?
Good question! I *believe* that the garbage collector will clean up anything (on some schedule) that does not have a pointer to it in scope. So a purely inline "return [...arrayToCopy, lastItem]" will stick around as long as you have a variable pointing to it. This now has me wondering about closures - perhaps its possible to have a reference to a closure that has an internal variable that's never used again, but the garbage collector doesn't know that. I'm not 100% sure about these things, so I hope someone else can chime in with a more complete answer!