Skip to content

Instantly share code, notes, and snippets.

@rajat1saxena
Created August 6, 2017 10:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajat1saxena/72cd3d0cd5801b9f0e2ef3afc0ac06e3 to your computer and use it in GitHub Desktop.
Save rajat1saxena/72cd3d0cd5801b9f0e2ef3afc0ac06e3 to your computer and use it in GitHub Desktop.
Efficiently write large files using Node.js
'use strict';
// this program will result in an "JavaScript heap out of memory"
const fs = require('fs');
const printer = function () {
const fil = fs.createWriteStream('file');
let i = 0;
const MAX_LIM = 1e6;
const writer = function () {
let result = true;
// Write to file until we get false as fil.write()'s
// result
while (i < MAX_LIM && result) {
result = fil.write(`Hello man: ${i}\n`);
// even if the result is false, our write has been probably
// written to the buffer. A false value denotes that the our last
// write has resulted in buffered data, crossing the highWaterMark.
// So, we have to increment the count.
i += 1;
}
// Add an event listener if the last write was not
// successful
if (i < MAX_LIM)
fil.once('drain', writer);
}
return writer;
}
const printty = printer();
printty();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment