嵌套for循环如何提高性能



这是我的for循环,它运行超过2个枚举,并将它们都发送到服务器,得到一个值,并通过另一个for循环计算值。我很确定它还可以改进。下面是代码:

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;

我的主要目标是减少时间,虽然我知道服务器将获得相同数量的查询,所以差异不会很大。谢谢。

您可以使用[Promise.all][1]。

下面是示例代码片段。

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

使用相同方法的另一种方法

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

一开始,我会把逻辑分成3部分:

  1. 准备异步调用
  2. 解析并行异步调用
  3. 应用我的业务逻辑
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;

正如其他答案所建议的那样,您可以使用Promise.all()。不需要太多的重构,你可以这样写:

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

相关内容

  • 没有找到相关文章

最新更新