0

Assume the following method(Just curious, I'm hopefully fail in js)

foo(0);
function foo(i){
 if(i<1000){
  setTimeout(function(){foo(i+1);}, 1000);
 }
 alert('I am foo');
}

So if I got it true, it should alerts around 1000 I am foos after 1000 seconds.
So doesn't it bother the browser about keeping the is in a sort of big stack? it should be too much I think or I'm wrong?

4
  • @dandavis - That will not be affected here. For example, here is one that iterates 60,000 times (1 minute). jsfiddle.net/twn8bmqq It will not crash your computer or lock your browser, it just takes a minute to execute. There are console logs every 10 seconds, and only 1 alert after the full minute. Commented Dec 12, 2014 at 23:00
  • chrome seems to allow 41864 recursive calls, firefox 41914, IE10 40027, before the stack is exceeded. you don't really have any because of the setTimeout, so you're fine. numbers based on function rec(){i++;return rec();} Commented Dec 12, 2014 at 23:00
  • @TravisJ: yeah, OP should be fine with a depth of 5, but a limit is worth knowing. Commented Dec 12, 2014 at 23:02
  • The actual call stack size is interesting, for sure. Commented Dec 12, 2014 at 23:02

4 Answers 4

2

Yes, you will alert 1000 "foo" messages with this code.

However, the browser will not get bothered by the amount of i variables. i is a local variable to foo and as a result, it is only kept around as long as foo is executing. Each time the setTimeout callback is finished executing, i and the entire function's footprint is eligible for garbage collection by the browser (which will happen at an opportune time).

The reason for this is that there is no preserved memory call stack here because of the use of setTimeout. setTimeout does not do anything with a returned value from foo and as a result there are no pointers in memory keeping that function nor its variable environment from being collected.

Sign up to request clarification or add additional context in comments.

Comments

1

Your call stack will look like this as your code goes through it's loop:

main() [local foo]
main() -> foo [local i = 0]
main()
(empty)
setTimeout()
setTimeout() -> foo [local i = 1]
setTimeout()
(empty)
setTimeout()
setTimeout() -> foo [local i = 2]
setTimeout()
(empty)

over and over until i equals 1000.

It happens this way because setTimeout is a browser api that waits n amount of time and then inserts the callback into the callback queue. The callback will then get picked up by the event loop and executed when the callstack is empty.

So, no, you aren't in any danger of overloading the browser with stacks or vars or whatever you were worried about. Your callstack will remain small because each time foo is executed, it fires off a setTimeout and returns. the i will stay in memory until the setTimeout's callback gets executed, at which point a new i will be created within the scope created by executing foo. That new i will stay in memory until the next setTimeout executes, on and on.

Here's a video that may help explain how this works. http://www.youtube.com/watch?v=8aGhZQkoFbQ

Comments

0

Firstly in javascript there is no "int" you have to say var i. And it gets alone the type for the declaration. If you want get Information about stack. You can use the debug console. And navigate to the call stacks. Its a nice feature you have also a nice overview. I prefer the Chromes Debugger. You can get there with pressing F12

1 Comment

Your welcome @user2889419. How to accept this answer ?
0

1000 is in a stack is not a really big size for a browser to deal with.

Moreover, there won't be more than 2 is at the same time (maybe even only one), with each function executing every second.

This code won't load 1000 calls to get triggered with an interval of 1 second, this will chain calls, preventing the stack to get bloated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.