Home > Mobile >  how nested for loop can be improved performance wise
how nested for loop can be improved performance wise

Time:10-14

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.

CodePudding user response:

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;
}

CodePudding user response:

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;

CodePudding user response:

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 };

  • Related