I want to process a large file line-by-line with Node.js. It's 100MB in size with 500,000 lines. I found this solution for reading lines in the input file
Now it's about writing each line into a new output file so I try
function readLines(input, func)
{
var remaining = "";
input.on("data", function(data)
{
remaining += data;
var index = remaining.indexOf("\n");
var last = 0;
while (index > -1)
{
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf("\n", last);
}
remaining = remaining.substring(last);
});
input.on("end", function()
{
if (remaining.length > 0)
{
func(remaining);
}
});
}
function write(data)
{
var written = output.write(data);
}
var fs = require("fs");
var input = fs.createReadStream("input.txt");
var output = fs.createWriteStream("output.txt", {flags: "w"});
readLines(input, write);
However the script is really slow, it takes over 1 hour to process the input file completely and costs a lot of CPU and RAM usage (the amount of CPU is 25 and the amount of memory usage is up to 200MB). So can anybody tell me if there is any way to optimize it?