I have large text files, which range between 30MB
and 10GB
. How can I count the number of lines in a file using Node.js
?
I hav
You can also use indexOf():
var index = -1;
var count = 0;
while ((index = chunk.indexOf(10, index + 1)) > -1) count++;
We can use indexOf to let the VM find the newlines:
function countFileLines(filePath){
return new Promise((resolve, reject) => {
let lineCount = 0;
fs.createReadStream(filePath)
.on("data", (buffer) => {
let idx = -1;
lineCount--; // Because the loop will run once for idx=-1
do {
idx = buffer.indexOf(10, idx+1);
lineCount++;
} while (idx !== -1);
}).on("end", () => {
resolve(lineCount);
}).on("error", reject);
});
};
What this solution does is that it finds the position of the first newline using .indexOf
. It increments lineCount
, then it finds the next position. The second parameter to .indexOf
tells where to start looking for newlines. This way we are jumping over large chunks of the buffer. The while loop will run once for every newline, plus one.
We are letting the Node runtime do the searching for us which is implemented on a lower level and should be faster.
On my system this is about twice as fast as running a for
loop over the buffer length on a large file (111 MB).
var fs=require('fs');
filename=process.argv[2];
var data=fs.readFileSync(filename);
var res=data.toString().split('\n').length;
console.log(res-1);`
If you use Node 8 and above, you can use this async/await pattern
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function fileLineCount({ fileLocation }) {
const { stdout } = await exec(`cat ${fileLocation} | wc -l`);
return parseInt(stdout);
};
// Usage
async someFunction() {
const lineCount = await fileLineCount({ fileLocation: 'some/file.json' });
}