浏览器 Javascript 堆栈大小限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7826992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:45:21  来源:igfitidea点击:

Browser Javascript Stack size limit

javascriptbrowserstacklimit

提问by guilhebl

I am getting some client-side Javascript stack overflow issues specifically in IE browser, this is happening inside a third party library that makes some function calls and for some reason they occasionally brake in IE only due to it's low stack limit.

我在 IE 浏览器中遇到了一些客户端 Javascript 堆栈溢出问题,这是在第三方库中发生的,该库进行了一些函数调用,出于某种原因,它们偶尔会在 IE 中刹车,只是因为它的堆栈限制较低。

I then coded a small test HTML to test the stack size limit for some browsers and found that IE8 has actually a small stack limit if compared to FF 7 or Chrome 14 running on a Laptop with Windows 7 OS, 8Gb RAM:

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

<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>

IE raises stack overflow when the values are around 3200, Firefox and Chrome can handle a very deep recursion if compared to IE.

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

I would like to know if there's a way to tie the stack-overflow exception with the Javascript function that raised it during runtime in IE or any other browser and if it could give the stacktrace with the chain of function in the stack at the moment the error was raised.

我想知道是否有一种方法可以将堆栈溢出异常与在 IE 或任何其他浏览器中运行时引发它的 Javascript 函数联系起来,以及它是否可以在堆栈中提供带有函数链的堆栈跟踪错误被提出。

回答by josh3736

Using a simple test:

使用一个简单的测试

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

Internet Explorer

IE浏览器

  • IE6: 1130
  • IE7: 2553
  • IE8: 1475
  • IE9: 20678
  • IE10: 20677
  • 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
  • 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

Google Chrome

谷歌浏览器

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

Safari

苹果浏览器

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

Opera

歌剧

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


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.

关于您的问题,请使用浏览器的开发人员工具查看堆栈。在 IE 8+ 中,点击F12,转到脚本选项卡,然后单击开始调试。抛出异常就会break,可以看到调用栈。您还可以使用 Chrome 的开发人员工具Ctrl+ Shift+ J

回答by Nikoloff

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.

这是特定于浏览器的,不仅是堆栈大小,还有优化,比如尾递归优化之类的东西。我想这里唯一可靠的事情是以一种不会将大量内容放入堆栈的方式进行编码,或者手动测试(深入阅读)每个浏览器的文档。毕竟,当您看到“递归过多”错误或类似错误时,您就已经知道您的代码确实有问题。