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

后端 未结 2 1834
清歌不尽
清歌不尽 2021-01-31 22:27

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 limitat

相关标签:
2条回答
  • 2021-01-31 22:58

    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

    0 讨论(0)
  • 2021-01-31 23:01

    An option could be passing --max-stack-size to node.

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

    For reference, use node -h

    --max-stack-size=val set max v8 stack size (bytes)

    Update

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

    node --stack-size=32000 app.js
    
    0 讨论(0)
提交回复
热议问题