My JSON file is very large to do this in memory (≈ 500MB) and there will be more than one files that I should read concurrently. Is there any way that I can do this in Node.js ? (with or without any external libraries)
const searchStream = (path, text = '') => new Promise((resolve, reject) => {
const inStream = fs.createReadStream(path);
const outStream = new Stream();
const rl = readline.createInterface(inStream, outStream);
const result = [];
let count = 0;
const regEx = new RegExp(text, 'i');
rl.on('line', (line) => {
if (line && line.search(regEx) >= 0) {
count++;
result.push(line);
}
});
rl.on('close', () => {
resolve({
result,
count,
});
});
rl.on('error', (err) => {
reject(err);
});
});