I'm currently looping through two folders (Old DB collection & New DB collection) containing multiple JSON files (Up to 7k files), each of which contains an array of objects (Up to 350 objects). The goal is to compare between the old object and the new object through the same ID and determine whether it has updates or if it is a new object.
The code below works fine, but it is extremely slow (40 objects/sec), especially when I need to verify millions of objects, which will take a long time.
const getUpdatedDocs = async () => {
const recentFiles = readdirSync(
`./firestore/recentCollections/${collection}`
);
const updatedDocs = [];
const addedDocs = [];
let docsCount = 0;
let batchCount = 0;
let batch = [];
const bar = new cliProgress.SingleBar({
format: `Checking |` + '{bar}' + '| {percentage}% || {value}/{total} ',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591'
});
bar.start(recentIds.length, 0, {
speed: 'N/A'
});
const findObject = async (id) => {
const currentFiles = readdirSync(
`./firestore/currentCollections/${collection}`
);
for await (const file of currentFiles) {
if (file === 'ids.json') {
continue;
}
const currentBatch = await readFileAsync(
`./firestore/currentCollections/${collection}/${file}`,
'utf8'
);
let object = currentBatch.filter((obj) => obj.docId === id);
if (object.length > 0) {
return object[0];
}
}
};
for await (const file of recentFiles) {
if (file === 'ids.json') {
continue;
}
const recentBatch = await readFileAsync(
`./firestore/recentCollections/${collection}/${file}`,
'utf8'
);
for await (const recentDoc of recentBatch) {
bar.increment();
if (docsCount < 350) {
let currentFile = await findObject(recentDoc.docId);
if (currentFile) {
if (!_.isEqual(currentFile, recentDoc)) {
docsCount++;
batch.push(recentDoc);
updatedDocs.push(recentDoc.docId);
}
} else {
docsCount++;
batch.push(recentDoc);
addedDocs.push(recentDoc.docId);
}
} else {
writeFileSync(
`./firestore/diffCollections/${collection}/${batchCount}.json`,
JSON.stringify(batch),
'utf8',
(err) => {
if (err) throw err;
}
);
batchCount++;
docsCount = 0;
batch = [];
}
}
}
if (batch.length > 0) {
writeFileSync(
`./firestore/diffCollections/${collection}/${batchCount}.json`,
JSON.stringify(batch),
'utf8',
(err) => {
if (err) throw err;
}
);
}
bar.stop();
return { updatedDocs, addedDocs };
};
What changes would you recommend to improve performance?