Processing large files with node.js

05 Feb, 2018JavaScript, Node.js

  1. Don't use recursion for things that can be done with loops. Recursion has a considerable performance gap compared to loops.

  2. There are only a few versions of node.js that have tail recursion optimization, and they need to be turned on by flag. The latest version does not ( ). The maximum call stack size for function calls is more than 10,000 value. In addition, tail recursion optimization in ES6 requires return.

  3. Use Stream as much as possible to read large files, you can specify the length of the block, and it will not block the event loop all the time.

  4. If you must use recursion to realize the logic, use setTimeout(func, 0), setImmediate(func), or process.nextTick(func) to transfer the depth of recursion from call stack to task queue .

  5. In addition to code optimization, the value that can be stored by local variables outside the loop does not need to be read in the loop with function calls.

Powered by Gatsby. Theme inspired by end2end.

© 2014-2021. Made withby mdluo.