Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
nodejs createWriteStream in nested for

I need to generate a huge number of combinations of string values, a few billion of lines and write them all in one large file to disk without the process being blocked.

At this moment it works until the file write buffer is filled, for little lists of words, when I try with a larger number of records the output files remains empty.


const fs = require('fs');

const words = fs.readFileSync('./1024words.txt').toString().split("\n");
//create array of 1024 words

let fileout
    , row = ''
    , wordA, wordB, wordC
    , totRows = Math.pow(words.length,3)*3  //combination of 3 words for 3 columns of output
    , count = 0;

for (let indexA = 0; indexA < words.length; indexA++) {
    wordA = words[ indexA ];

    fileout = fs.createWriteStream(`${wordA}.csv`); //new output file

    for (let indexB = 0; indexB < words.length; indexB++) {
        wordB = words[ indexB ];

        for (let indexC = 0; indexC < words.length; indexC++) {
            wordC = words[ indexC ];

            row = `${wordA} ${wordB} ${wordC}\n`;

            count++;

            process.stdout.write(`${count} of ${totRows} written: ${row}`);

            fileout.write(row);
        }
    }

    fileout.end();
    fileout.close();
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you're missing a backpressure handling

further reading here

https://nodejs.org/en/docs/guides/backpressuring-in-streams/

add this function

 function writeToStream(stream, data) {
        const canContinue = stream.write(data);
        if (!canContinue) {
            stream.pause()
            stream.once('drain', () => {
                stream.resume()
            })
        }
    }

full code :

const fs = require('fs');

const words = fs.readFileSync('./1024words.txt').toString().split("\n");
//create array of 1024 words

let fileout
    , row = ''
    , wordA, wordB, wordC
    , totRows = Math.pow(words.length, 3) * 3  //combination of 3 words for 3 columns of output
    , count = 0;

for (let indexA = 0; indexA < words.length; indexA++) {
    wordA = words[indexA];

    fileout = fs.createWriteStream(`${wordA}.csv`); //new output file

    for (let indexB = 0; indexB < words.length; indexB++) {
        wordB = words[indexB];

        for (let indexC = 0; indexC < words.length; indexC++) {
            wordC = words[indexC];

            row = `${wordA} ${wordB} ${wordC}\n`;

            count++;
            writeToStream(process.stdout, `${count} of ${totRows} written: ${row}`)
            writeToStream(fileout, row)
        }
    }

    fileout.end();
    fileout.close();
}

function writeToStream(stream, data) {
    const canContinue = stream.write(data);
    if (!canContinue) {
        stream.pause()
        stream.once('drain', () => {
            stream.resume()
        })
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!