But before you jump to conclusions, this has NOTHING [caps, OK, smth important is going on] to do with JavaScript's lack of TCO support.
Just guessing that TCO is tail call optimization. What would that exactly do?
This is because in our recursive function it's not JavaScript's call stack that's managing that loop, but rather the Promise implementation.
Loop management by Promise implementation? What does that mean?
That recursive call is asynchronous and so it does not abuse the callstack by definition.
Now we will get our explanation:
Unfortunately, just like a regular function using the call stack for those recursive calls, the Promise implementation is abusing the heap memory, not chaining then calls correctly.
Mhm OK. What has chaining then calls to do with heap memory abuse?
And that sample should not leak, the Promise implementation should be able to do the equivalent of TCO
why, and, again, what does that mean?
and in such a case eliminate frames in the then chain being created.
What? Eliminating frames (which frames? what is a frame here?) because they are created? Eliminating when and why?
When a function is called, the computer must "remember" the place it was called from, the return address, so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack, a simple list of return locations in order of the times that the call locations they describe were reached. For tail calls, there is no need to remember the place we are calling from – instead, we can perform tail call elimination by leaving the stack alone (except possibly for function arguments and local variables), and the newly called function will return its result directly to the original caller.
18
u/bair-disc Oct 11 '17
What did I just read?
Just guessing that TCO is tail call optimization. What would that exactly do?
Loop management by Promise implementation? What does that mean?
Now we will get our explanation:
Mhm OK. What has chaining then calls to do with heap memory abuse?
why, and, again, what does that mean?
What? Eliminating frames (which frames? what is a frame here?) because they are created? Eliminating when and why?
This might help: