Skip to content

Commit 312c38c

Browse files
authored
chore: optimized stats calculation by fetching contribution data in parallel using Promise.all (#416)
- Replaced sequential for loop with Promise.all to fetch contributions for each year in parallel. - Utilized Array.reduce to aggregate total commit contributions, restricted contributions, and pull request review contributions. - Improved performance by reducing the overall time to retrieve data from GitHub API.
1 parent 7454601 commit 312c38c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/fetcher/stats.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,26 @@ async function stats(username: string): Promise<User> {
252252
const startYear = await getUserJoinYear(username);
253253
const endYear = new Date().getFullYear();
254254

255-
let TotalCommitContributions = 0;
256-
let RestrictedContributionsCount = 0;
257-
let TotalPullRequestReviewContributions = 0;
258-
255+
const contributionPromises = [];
259256
for (let year = startYear; year <= endYear; year++) {
260-
const contributions = await fetchContributions(username, year);
261-
TotalCommitContributions += contributions.totalCommitContributions;
262-
RestrictedContributionsCount += contributions.restrictedContributionsCount;
263-
TotalPullRequestReviewContributions += contributions.totalPullRequestReviewContributions;
257+
contributionPromises.push(fetchContributions(username, year));
264258
}
265259

260+
const contributions = await Promise.all(contributionPromises);
261+
262+
const TotalCommitContributions = contributions.reduce(
263+
(total, contribution) => total + contribution.totalCommitContributions,
264+
0
265+
);
266+
const RestrictedContributionsCount = contributions.reduce(
267+
(total, contribution) => total + contribution.restrictedContributionsCount,
268+
0
269+
);
270+
const TotalPullRequestReviewContributions = contributions.reduce(
271+
(total, contribution) => total + contribution.totalPullRequestReviewContributions,
272+
0
273+
);
274+
266275
const data = await axios({
267276
method: "post",
268277
url: "https://api.github.com/graphql",

0 commit comments

Comments
 (0)