Skip to content

Commit 1279a1e

Browse files
committed
perf: faster in bun
1 parent 07c954f commit 1279a1e

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@
6565
"specifier-backward": "^0.0.4",
6666
"typescript": "^5.2.2",
6767
"vite": "^4.4.10"
68+
},
69+
"dependencies": {
70+
"std-env": "^3.7.0"
6871
}
6972
}

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve } from "node:path";
2+
import { runtime } from "std-env";
23
import prettyBytes from "pretty-bytes";
34
import { lstat, readdir } from "node:fs/promises";
45

@@ -39,19 +40,44 @@ export async function getFolderSize(
3940
options?: Options,
4041
) {
4142
const { loose = false } = options || {};
43+
let total = 0;
44+
const sumTotal = (size: number) => total += size;
45+
const getFileSize = loose ? looseGetFileSize : strictGetFileSize;
46+
47+
// bun (use recursive)
48+
if (runtime === "bun") {
49+
const dirents = await readdir(base, {
50+
recursive: true,
51+
withFileTypes: true,
52+
});
53+
54+
if (dirents.length === 0) {
55+
return mayBeWithPrettyBytes();
56+
}
57+
58+
const promises = dirents.map(async (dirent) => {
59+
if (!dirent.isFile()) {
60+
return;
61+
}
62+
const size = await getFileSize(resolve(base, dirent.name));
63+
sumTotal(size);
64+
});
65+
66+
await Promise.all(promises);
67+
68+
return mayBeWithPrettyBytes();
69+
}
70+
4271
const dirents = await readdir(base, {
4372
withFileTypes: true,
4473
});
74+
4575
if (dirents.length === 0) {
46-
return 0;
76+
return mayBeWithPrettyBytes();
4777
}
4878

4979
const promises: Array<Promise<number>> = [];
5080

51-
let total = 0;
52-
const sumTotal = (size: number) => total += size;
53-
const getFileSize = loose ? looseGetFileSize : strictGetFileSize;
54-
5581
for (const dirent of dirents) {
5682
if (dirent.isFile()) {
5783
const path = resolve(base, dirent.name);
@@ -70,9 +96,9 @@ export async function getFolderSize(
7096

7197
await Promise.all(promises);
7298

73-
if (!pretty) {
74-
return total;
75-
}
99+
return mayBeWithPrettyBytes();
76100

77-
return prettyBytes(total);
101+
function mayBeWithPrettyBytes() {
102+
return pretty ? prettyBytes(total) : total;
103+
}
78104
}

wasm/main.wasm

72.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)