Skip to content

Instantly share code, notes, and snippets.

@shinout
Created April 15, 2011 04:04
Show Gist options
  • Save shinout/921121 to your computer and use it in GitHub Desktop.
Save shinout/921121 to your computer and use it in GitHub Desktop.
fs.createWriteStream with large data
var fs = require('fs');
var stream = fs.createWriteStream(__dirname + '/out.txt');
var str = 'hogehogefugafugafoobarpiyo[';
for ( var i=0; i < 1000000000; i++) { // 1Gtimes
stream.write(str + i + ']\n');
}
stream.on('drain', function() {
console.log('drain'); // never happen
});
@koichik
Copy link

koichik commented Apr 15, 2011

var fs = require('fs');
var stream = fs.createWriteStream(__dirname + '/out.txt');
var str = 'hogehogefugafugafoobarpiyo[';
var i = 0;

stream.on('drain', function() {
  write();
});
write();

function write() {
  while (i < 1000000000) { // 1Gtimes
    if (!stream.write(str + (i++) + ']\n')) {
      return;
    }
  }
  stream.end();
}

@shinout
Copy link
Author

shinout commented Apr 15, 2011

I totally forgot the fact that node.js is a single thread model...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment