Created
August 6, 2017 10:05
-
-
Save rajat1saxena/72cd3d0cd5801b9f0e2ef3afc0ac06e3 to your computer and use it in GitHub Desktop.
Efficiently write large files using Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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