Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!