admin管理员组文章数量:1327843
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?
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?
- 1 It's curious that if I run this code with fewer iterations (i.e. 40763) the error is gone until a page refresh. – Роман Парадеев Commented Apr 26, 2016 at 18:23
- In Chrome on Windows v49.0.2623.87 m I am getting RangeError: Maximum call stack size exceeded when it is exceeded. And return true - if it's not. – Yuriy Galanter Commented Apr 26, 2016 at 18:48
- Perhaps this and this helps? – Ivan Sivak Commented May 2, 2016 at 12:31
-
1. why do you need it? (you could implement it using queue) 2. why do you not use
try...catch
? – Pavel Gatnar Commented May 2, 2016 at 12:47
2 Answers
Reset to default 9 +100The 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.
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.
本文标签: javascriptRecursionCall stack fails to pop when testing the maximum stack sizeStack Overflow
版权声明:本文标题:javascript - Recursion - Call stack fails to pop when testing the maximum stack size - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220528a2435372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论