首页 > 解决方案 > NodeJS 和 CallStack 大小保护

问题描述

NodeJS 是否能够查看或设置任何限制(在运行时或通过 CLI 标志?)调用堆栈的大小?

如果我创建一个看起来像这样的程序并运行它

const recurse = (num=0) => {
  console.log('level ' + num);
  recurse(++num)
}
recurse()

Node 最终会因为这个错误退出程序

//...
level 12021
_stream_readable.js:872
Readable.prototype.removeListener = function(ev, fn) {
                                            ^

RangeError: Maximum call stack size exceeded
    at WriteStream.Readable.removeListener (_stream_readable.js:872:45)
    at write (console.js:172:12)
    at Console.log (console.js:200:3)
    at recurse (/path/to/so.js:2:11)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
C02V30M6HTDG:test-express-app astorm$ cat so.js 
const recurse = (num=0) => {
  console.log('level ' + num);
  recurse(++num)
}
recurse()

它救助的水平/深度各不相同(通常在 10,000 - 13,000 范围内)。此错误消息告诉我调用堆栈大小(不是深度)是问题所在,但不清楚这里的大小(我假设节点数据结构大小是内部的)。

我想知道的是——节点是否允许我

  1. 查看/设置此调用堆栈保护启动的大小
  2. 查看/设置调用堆栈的深度限制。

我没有要解决的任何特定问题,我只是想了解 NodeJS 与我使用过的其他语言相比,给了我多少控制权。

标签: node.jscallstack

解决方案


推荐阅读