javascript nodeJs 大数组处理抛出 RangeError: Maximum call stack size exceeded

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20243945/
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-10-27 18:04:49  来源:igfitidea点击:

nodeJs huge array processing throws RangeError: Maximum call stack size exceeded

javascriptarraysnode.jsmemory-management

提问by Alber

This is part of code to proceed big number of entries ( originally its works with file system and make some operations with files) . Is there any nice way to bypass the limitation and prevent throwing of RangeError: Maximum call stack size exceeded( As for now it allows me to iterate about 3000 items )

这是处理大量条目的代码的一部分(最初它与文件系统一起工作并对文件进行一些操作)。有什么好的方法可以绕过限制并防止抛出RangeError: Maximum call stack size exceeded(至于现在它允许我迭代大约 3000 个项目)

var async = require('async')
    , _u = require('underscore')

var tifPreview = function (item, callback) {
    console.log(item)
    return callback();
}

var tifQueue = async.queue(tifPreview, 2)

tifQueue.push(_u.range(0, 5000, 1))

回答by bevacqua

An option could be passing --max-stack-sizeto node.

一个选项可能是传递--max-stack-sizenode.

node --max-stack-size 32000 app.js

For reference, use node -h

作为参考,使用 node -h

--max-stack-size=valset max v8 stack size (bytes)

--max-stack-size=val设置最大 v8 堆栈大小(字节)

Update

更新

Even though help prints it as --max-stack-size, in node -vv0.10.x+ you need to use --stack-sizeinstead.

尽管 help 将其打印为--max-stack-size,但node -vv0.10.x您需要在+ 中使用它--stack-size

node --stack-size=32000 app.js

回答by Linus Unneb?ck

The problem is that you are making to many function calls. Setting the stack-sizeto a higher value will only increase the number of items you can handle, not solve the actual problem.

问题是您要进行许多函数调用。将 设置stack-size为更高的值只会增加您可以处理的项目数量,并不能解决实际问题。

You are calling the next iteration straight from your function, which makes it a recursive function. It's a bit hard to spot since it's going thru async.

您直接从您的函数调用下一次迭代,这使其成为递归函数。有点难发现,因为它正在通过async

This code should work:

此代码应该工作:

var tifPreview = function (item, callback) {
  console.log(item);

  // defer the callback
  setImmediate(callback);
}

Read more about the setImmediatefunction here: http://nodejs.org/api/timers.html#timers_setimmediate_callback_arg

setImmediate此处阅读有关该函数的更多信息:http: //nodejs.org/api/timers.html#timers_setimmediate_callback_arg