2

I currently have a csv file that is 1.3 million lines. I'm trying to parse this file line by line and run a processes on each line. The issue I am running into, is I run out of heap memory. I've read online and tried a bunch of solutions to not store the entire file into memory, but it seems nothing is working. Here is my current code:

const readLine = createInterface({
  input: createReadStream(file),
  crlfDelay: Infinity
});

readLine.on('line', async (line) => {
  let record = parse2(`${line}`, {
    delimiter: ',',
    skip_empty_lines: true,
    skip_lines_with_empty_values: false
  });

  // Do something with record

  index++;
  if (index % 1000 === 0) {
    console.log(index);
  }
});

// halts process until all lines have been processed
await once(readLine, 'close');

This starts off strong, but slowly the heap gets filled, and I run out of memory and the program crashes. I'm using a readstream, so I don't understand why the file is filling the heap.

Gary Holiday
  • 2,947
  • 2
  • 25
  • 67

2 Answers2

1

Try using the library csv-parser https://www.npmjs.com/package/csv-parser

const csv = require('csv-parser');
const fs = require('fs');

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (row) => {
    console.log(row);
  })
  .on('end', () => {
    console.log('CSV file successfully processed');
  });

Taken from: https://stackabuse.com/reading-and-writing-csv-files-with-node-js/

Daniel B.
  • 2,253
  • 1
  • 9
  • 22
  • Tried it, that exact code. Still runs out of heap memory. That's why I tried switching to readline. Getting worse performance now. – Gary Holiday Feb 11 '20 at 09:41
0

I had tried something similar for file for ~2GB and it worked without any issue with EventStream

var fs = require('fs');
var eventStream = require('event-stream');

fs
.createReadStream('veryLargeFile.txt')
.pipe(eventStream.split())
.pipe(
    eventStream
    .mapSync(function(line) {
        // Do something with record `line`
    }).on('error', function(err) {
        console.log('Error while reading file.', err);
    })
    .on('end', function() {
        // On End
    })
)

Please try and let me know if it helps

Sagar Chilukuri
  • 1,250
  • 2
  • 16
  • 27