Skip to content

Commit e3834f5

Browse files
committed
Improve performance of DownloadUtils.getTotalDownloadSizeInBytes
Using Promise.all rather than checking for cached versions of the mods in a loop reduces execution time roughly from 50% to 80%. Granted, we were talking about tens of milliseconds to begin with even for large mod packs, but faster is faster.
1 parent fbe5cee commit e3834f5

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/utils/DownloadUtils.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,21 @@ export function addSolutionsToError(err: R2Error): void {
2626
/**
2727
* Returns the total download size for a list of mods.
2828
* If ignoreCache is set to true, all of the mods are included in the total download size.
29-
* Otherwise, it checks if the files are already downloaded and returns the
29+
* Otherwise, it checks if the files are already downloaded and returns the
3030
* total size of the files that actually need to be downloaded.
3131
*
3232
* @param combos The mods that need to be downloaded.
3333
* @param ignoreCache Whether to ignore the cache and include all mods in the total download size.
3434
* @returns The total download file size of the mods to download (in bytes).
3535
*/
3636
export async function getTotalDownloadSizeInBytes(combos: ThunderstoreCombo[], ignoreCache: boolean): Promise<number> {
37-
let filteredCombos = [];
38-
if (ignoreCache) {
39-
filteredCombos = combos;
40-
} else {
41-
for (const combo of combos) {
42-
if (!(await isVersionAlreadyDownloaded(combo))) {
43-
filteredCombos.push(combo);
44-
}
45-
}
46-
}
47-
return filteredCombos.reduce((total, combo) => total + combo.getVersion().getFileSize(), 0);
37+
const requiresDownload = ignoreCache
38+
? Array(combos.length).fill(true)
39+
: await Promise.all(combos.map((combo) => !isVersionAlreadyDownloaded(combo)));
40+
41+
return combos
42+
.filter((_combo, index) => requiresDownload[index])
43+
.reduce((total, combo) => total + combo.getVersion().getFileSize(), 0);
4844
}
4945

5046
/**

0 commit comments

Comments
 (0)