栈大小限制

我得到了一些客户端 Javascript 堆栈溢出问题,特别是在 IE 浏览器, 这发生在一个第三方库中,该库进行一些函数调用,由于某些原因,它们偶尔会因为 IE 的低堆栈限制而停止。

然后我编写了一个小测试 HTML 来测试一些浏览器的堆栈大小限制,发现 IE8实际上有一个小的堆栈限制,如果与运行在笔记本电脑上的 Windows 7操作系统,8Gb 内存的 FF 7或 Chrome 14相比:

<html>
<body>


<!-- begin Script: -->
<script type="text/javascript">


function doSomething(){


var i = 3200;
doSomethingElse(i);


}


function doSomethingElse(i){
if (i == 0) return -1;
doSomethingElse(i-1);
}


doSomething();


</script>
<!-- END OF PAGE -->


</body>
</html>

当值在3200左右时,IE 会引起堆栈溢出,如果与 IE 相比,Firefox 和 Chrome 可以处理非常深的递归。

我想知道是否有一种方法可以将堆栈溢出异常与在 IE 或其他浏览器中运行时引发堆栈溢出异常的 Javascript 函数绑定在一起,以及是否可以在出现错误的时候将堆栈跟踪与堆栈中的函数链绑定在一起。

63389 次浏览

This is browser specific, not only the stack size, but also optimizations, things like tail recursion optimization and stuff. I guess the only reliable thing here is to code in a way that doesn't put tons of stuff into the stack, or manually testing(reading deep into the documentation of) each browser. After all, when you see the "too much recursion" error or similar you already know there's something really wrong with your code.

Using a simple test:

var i = 0;
function inc() {
i++;
inc();
}
    

try {
inc();
}
catch(e) {
// The StackOverflow sandbox adds one frame that is not being counted by this code
// Incrementing once manually
i++;
console.log('Maximum stack size is', i, 'in your current browser');
}

Internet Explorer

  • IE6: 1130
  • IE7: 2553
  • IE8: 1475
  • IE9: 20678
  • IE10: 20677

Mozilla Firefox

  • 3.6: 3000
  • 4.0: 9015
  • 5.0: 9015
  • 6.0: 9015
  • 7.0: 65533
  • 8b3: 63485
  • 17: 50762
  • 18: 52596
  • 19: 52458
  • 42: 281810
  • 89: 10746
  • 91: 26441

Google Chrome

  • 14: 26177
  • 15: 26168
  • 16: 26166
  • 25: 25090
  • 47: 20878
  • 51: 41753
  • 93: 13903

Safari

  • 4: 52426
  • 5: 65534
  • 9: 63444
  • 14: 45606

Safari iOS

  • 15: 7909

Opera

  • 10.10: 9999
  • 10.62: 32631
  • 11: 32631
  • 12: 32631
  • 78: 13908

Edge

  • 87: 13970
  • 93: 13903

Yandex

  • 21: 13909

In regard to your question, use your browser's developer tools to see the stack. In IE 8+, hit F12, go to the Script tab, and click Start Debugging. It will break when an exception is thrown, and you can see the call stack. You can also use Chrome's developer tools, Ctrl+Shift+J.