Actually, you don't create a recursive function there. By invoking setTimeout
its not calling itself anymore. The only closure created here is the anonymous function for setTimeout
, once that is executed the garbage collector will recognize that the reference to the previous instance can get cleaned up. This probably will not happen instantly, but you definetly can't create a stack overflow
using that. Lets view this example:
function myFunc() {
var bigarray = new Array(10000).join('foobar');
setTimeout(myFunc, 200);
}
myFunc();
Watching the memory usage from your browser now. It'll grow constantly, but after a while (20-40 seconds for me) it'll get completely cleaned again. On the other hand, if we create a real recursion like this:
function myFunc() {
var bigarray = new Array(10000).join('foobar');
myFunc();
}
myFunc();
Our memory usage will grow, browsers locks down and we finally create that stack overflow
. Javascript does not implement Tail recursion, so we will end up with an overflow in all cases.
update
it lookes like I was wrong in my first example. That behavior is only true if no function-context is invoked (using a anonymous function for instance). If we re-write that like
function myFunc() {
var bigarray = new Array(10000).join('foobar');
setTimeout(function() {
myFunc();
}, 200);
}
myFunc();
Browser memory seems not to get released any more. Grows forever. That might be because any inner-closured keeps a reference to bigarray
. But anyway.