Reputation: 67207
Basically call stack will start to pop out the function calls one by one when the last-in function call returns. But when ever I try to create a call stack nearer to the size of its maximum, An uncaught expression
is getting raised.
//Code for testing the stack size
var cnt = 0;
function test(){
//Max stack size is nearer to ~41800
if(cnt++ == 41763){
console.log('finished');
return true;
}
return test();
}
test();
So the above code is throwing an exception for me in chromium Version 49.0.2623.112 m like below,
Uncaught exception
< true
Please not that the above error has no message in it. My question here is,
The last function call in the stack has returned true
that means the stack size was not exceeded. why didn't the other function calls in that stack was not returned? And what is the reason for this blank exception message?
Upvotes: 15
Views: 416
Reputation: 3307
The console.log is not in specification of javascript, so the behavior is undefined, and can be changed from release to release. So something that happens in your version maybe doesn't happen in ours.
Here is the most likely explanation for what happened: It is certain that console.log will increase the size of stack, and because is the last statement you call before the return, it can produce a Maximum call stack
sometimes because you are very close to the limit. That Maximum call can be happen inside the console.log code(who calls something else), and how this error will handle it, it depends on the code of console.log. It seems like the code inside the console.log throws a Uncaught exception when an error happens. Now when you catch an error with try, code continues, that's why the true appears.
Here is an example in jsfiddle where I override the console.log and the results appear in the HTML. You can play by removing the override code of console.log to see how things change. Try this and tell us if the result seems strange again.
It's worth notice that when the Maximum call stack size exceeded
error appears it depends on the size of the stack frame too (local variables).
Note: In ECMAScript 6 spec, if a function call is the last action in a function, it will not go in stack, but it will run "immediately", so your code will run without an error for all the numbers, no matter what number you put.
Upvotes: 3
Reputation: 1838
The problem here is
console.log('finished');
this adds some extra functions to the call stack that will let the limit exceed, but through exception handling your code gets executed anyway.
Try to run it without console.log and you see you get to the limit and see either a true or an exception.
Upvotes: 9