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

159
Views
how nested for loop can be improved performance wise

Here is my for loop, It runs over 2 enums, and send both of them to the server, get's a value, and calculate the value through another for loop. Pretty sure it can be improved. here is the code:

  const paths = [];
  for await (let category of Object.values(CategoriesEnum)) {
    for await (let format of Object.values(FormatsEnum)) {
      const totalPosts = await getPagesCount(category, format);
      const totalPages = Math.ceil(totalPosts.offsetPagination.total / 12);
      for (let page = 1; page <= totalPages; page++) {
        paths.push({ params: { category: category , format: format page: page } });
      }
    }
  }
return paths;

My main goal is to reduce the time, although i understand that the server will get the same amount of queries so the differences won't be huge. Thanks.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use [Promise.all][1].

Here's the sample snippet.

let promises = [];
let paths = [];

Object.values(CategoriesEnum).forEach(category => {
    Object.values(FormatsEnum).forEach(format => {
        promises.push(getPagesCount(category, format))
    })
});

const totalPostCounts = await Promise.all(promises);



totalPostCounts.forEach(totalPosts => {
    const totalPages = Math.ceil(totalPosts.offsetPagination.total / 12);
    for (let page = 1; page <= totalPages; page++) {
        paths.push({ params: { category: category, format: format, page: page } });
    }
});

return paths;




[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Another Approach using same Methods

let promises = [];
let paths = [];


/*
Gather Api calls
*/
Object.values(CategoriesEnum).forEach(category => {
    Object.values(FormatsEnum).forEach(format => {
        promises.push(getPath(category, format))
    })
});

// wait for the call
const pathsArray = await Promise.all(promises);

// flat the array
pathsArray.forEach(pathArray => {
    paths = paths.concat(pathArray)
});


async function getPath(category, format) {
    let paths = [];

    const totalPosts = await getPagesCount(category, format);
    const totalPages = Math.ceil(totalPosts.offsetPagination.total / 12);
    for (let page = 1; page <= totalPages; page++) {
        paths.push({ params: { category: category, format: format, page: page } });
    }

    return paths;
}
about 4 years ago · Juan Pablo Isaza Report

0

For the beginning, I would split logic into 3 parts:

  1. Prepare async calls
  2. Resolve async calls in parallel
  3. Apply my business logic
const pagesPromises = []
for  (let category of Object.values(CategoriesEnum)) {
  for  (let format of Object.values(FormatsEnum)) {
    pagesPromises.push(getPagesCount(category, format))
  }
}

const totalPostsResults = await Promise.all(pagesPromises)

const paths = [];
totalPostsResults.forEach(totalPosts => {
  const totalPages = Math.ceil(totalPosts.offsetPagination.total / 12);
  for (let page = 1; page <= totalPages; page++) {
    paths.push({ params: { category: category , format: format page: page } });
  }
})

return paths;
about 4 years ago · Juan Pablo Isaza Report

0

As other answers suggest, you can use Promise.all(). Without much refactoring you can write:

const getStaticPaths = async () => {
  const paths = [];
  const promises = []; // : Array<Promise<void>> in ts

  Object.values(CategoriesEnum).forEach((category) => {
    Object.values(FormatsEnum).forEach((format) => {
      promises.push(async () => {

        const totalPosts = await getPagesCount(category, format); // <-- confusing name though, I'd prefer `getPostsCount` instead
        const totalPages = Math.ceil(totalPosts.offsetPagination.total / 12);
        for (const page = 1; page <= totalPages; ++page)
          paths.push({ params: { category: category, format: format, page: page } });

      });
    });
  });

  await Promise.all(promises);
  return { paths };
};

export { getStaticPaths };

about 4 years ago · Juan Pablo Isaza Report
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!