Actualmente estoy recorriendo dos carpetas (colección de base de datos antigua y colección de base de datos nueva) que contienen varios archivos JSON (hasta 7k archivos), cada uno de los cuales contiene una matriz de objetos (hasta 350 objetos). El objetivo es comparar entre el objeto antiguo y el objeto nuevo a través del mismo ID y determinar si tiene actualizaciones o si es un objeto nuevo.
El siguiente código funciona bien, pero es extremadamente lento (40 objetos por segundo), especialmente cuando necesito verificar millones de objetos, lo que llevará mucho tiempo.
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 }; };¿Qué cambios recomendaría para mejorar el rendimiento?