Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
Seeking a method to improve the performance of this JavaScript function

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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda