I was playing around with node.js and I found that this simple program is running incredibly slowly, and I didn’t even wait around to see how long it took after 3 minutes had passed.
var fs = require ('fs')
var s = fs.createWriteStream("test.txt");
for (i = 1; i <= 1000000; i++)
s.write(i+"\n");
s.end()
I experimented using different values, and found that while 1-112050 takes 3 seconds, 1-112051 takes over a minute. This sudden dropoff is strange. The same program in python, or the equivalent shell script ‘seq 1 112051` runs in a reasonable amount of time (0-2 seconds).
Note that this node.js app runs much faster:
var fs = require('fs')
, s = []
for (var i = 1; i <= 1000000; i++) s.push(i.toString())
s.push('')
fs.writeFile('UIDs.txt', s.join('\n'), 'utf8')
Can anyone explain to me why node.js behaves this way, and why the dropoff is so sudden?
It’s a buffer that gets filled up. Each write will return
trueorfalsedepending 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.
Output: