Let\'s say I have a long running loop:
// Let\'s say this loop takes 10 seconds to execute
for(let i = 0; i <= 1000000; ++i) {
const garbage = { i };
As a concept the garbage collector works in a separate thread since in this way it will not block the main thread (UI thread in most cases).
As for your example, there is no problem for the garbage collection thread running in "parallel" to this loop since the value there const garbage = {key: i}
will not get removed as long as it is being referenced.
Also note that there are several generations that the garbage collector passes your values through before removing them completely.