1

Write a Node.js code to write a string "Hello world" 10^5 times in a file using stream writer in Node.js.

I have tried using loops but there is out of memory error and the system hangs.

Any better method?

var fs = require('fs');
var wstream = fs.createWriteStream('D:\\myOutput.txt');
for(var i=0;i<10^5;i++){
wstream.write('Hello world!\n');
}
wstream.end();
2
  • 10^5, i.e. 10 XOR 5 evaluates to 15. You probably want 10 ** 5 (or Math.pow(10, 5)). Commented Sep 8, 2020 at 17:18
  • I recommend you reading this stackoverflow.com/questions/37579605/…. Commented Sep 8, 2020 at 20:36

1 Answer 1

1

Node.js is a single-threaded execution environment, when you write data in a loop the node.js will not get a chance to execute other tasks, as you can see from the documentation(https://nodejs.org/docs/latest-v12.x/api/stream.html#stream_writable_write_chunk_encoding_callback) nodejs does not guarantee that all the data will be written once you call write, it can be buffered, if you want node to stay responsive, you have to free up the event loop (https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#what-is-the-event-loop) for other tasks, an example where you can do both is as follows. hope this clears things up

var fs = require('fs');
var wstream = fs.createWriteStream('myOutput.txt');
wstream.on('error', function (err) {
    console.log(err);
});



async function write()
{
for(var i=0;i<10^5;i++){
await writeStream(wstream, 'Hello world!\n');
}
wstream.end();
}

async function writeStream (stream, data) {
  if (!stream.write(data)) {
   return new Promise((resolv, reject) => {
    stream.once('drain', () => {resolv();});
   });
  } else {
    return Promise.resolve();
  }
}

write();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.