巨大的NodeJS阵列处理引发RangeError:最大调用堆栈大小超过 [英] nodeJs huge array processing throws RangeError: Maximum call stack size exceeded

查看:1862
本文介绍了巨大的NodeJS阵列处理引发RangeError:最大调用堆栈大小超过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code的一部分进行条目的数量大(最初的作品与文件系统,并提出一些操作与文件)。有没有绕过的限制和prevent投掷任何好办法的的RangeError:最大调用堆栈大小超过
(至于现在它让我重复约3000项)

  VAR异步=要求(异步)
    ,_u =要求('下划线')VAR TIF preVIEW =函数(项目,回调){
    的console.log(项目)
    返回回调();
}VAR tifQueue = async.queue(TIF preVIEW,2)tifQueue.push(_u.range(0,5000,1))


解决方案

的问题是,你正在为许多函数调用。设置堆栈大小到一个更高的价值只会增加你可以处理的项目数,不解决实际问题。

您是直接从您的功能,这使得它递归函数调用的下一次迭代。这是一个有点难以发现,因为它会直通异步

这code应该工作:

  VAR TIF preVIEW =函数(项目,回调){
  的console.log(项目);  //推迟回调
  setImmediate(回调);
}

了解更多关于这里的 setImmediate 函数:
<一href=\"http://nodejs.org/api/timers.html#timers_setimmediate_callback_arg\">http://nodejs.org/api/timers.html#timers_setimmediate_callback_arg

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 )

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

解决方案

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

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.

This code should work:

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

  // defer the callback
  setImmediate(callback);
}

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

这篇关于巨大的NodeJS阵列处理引发RangeError:最大调用堆栈大小超过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆