Sunday 30 September 2018

Write 1 million times

I see there's an example on the documentation for this but I was wondering if there was a faster way? To write a million + times to a filefunction writeOneMillionTimes(writer, data, encoding, callback) { let i = 1000000; write(); function write() { let ok = true; do { i--; if (i === 0) { // last time! writer.write(data, encoding, callback); } else { // see if we should continue, or wait // don't pass the callback, because we're not done yet. ok = writer.write(data, encoding); } } while (i > 0 && ok); if (i > 0) { // had to stop early! // write some more once it drains writer.once('drain', write); } } }

Submitted October 01, 2018 at 02:59AM by jsscrub

No comments:

Post a Comment