Why does this node.js loop run slowly after 112050 iterations? Why does this node.js loop run slowly after 112050 iterations? node.js node.js

Why does this node.js loop run slowly after 112050 iterations?


This happens because for cycle is synchronous, but Writable.write() is not. For your example s.write creates one million chuncks queue. That causes more than one million function calls (like this) to process this queue. So, Writable.write is not designed for small chunks. You can check sources of Writable.write to get more info about it.


It's a buffer that gets filled up. Each write will return true or false depending on the state of the kernel buffer.

If you start listen to the return code and use the drain event, it will at least be consistant in speed.

var fs = require ('fs') function runTest(stop) {  var s = fs.createWriteStream("test.txt");  var startTime = Date.now();  var c = 1;  function doIt() {    while (++c <= stop) {      if (!s.write(c+"\n")) {        s.once('drain', doIt);        return;      }    }    s.end();    var diffTime = Date.now() - startTime;    console.log(stop+': took '+diffTime+'ms, per write: '+(diffTime/stop)+'ms')  }  doIt();}runTest(10000);runTest(100000);runTest(1000000);runTest(10000000);runTest(100000000);

Output:

$ node test.js10000: took 717ms, per write: 0.0717ms100000: took 5818ms, per write: 0.05818ms1000000: took 42902ms, per write: 0.042902ms10000000: took 331583ms, per write: 0.0331583ms100000000: took 2542195ms, per write: 0.02542195ms


I believe this may be environment specific, In what context are you coding this?eg, initially I thought it was for a website, but it involves writing files which throws me off.

Otherwise, working with file systems works funny depending on implementations, I'm not one to blame programming languages but I really don't know how javascript treats file IO on your particular system, performance in file IO is a science in its own right, possibly as old as computer science itself.