Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda