diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..4ec68fe --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-06 - [Performance Optimization] Filesystem Traversal +**Learning:** In local-first CLI applications that extensively traverse the filesystem (like vspec), using `fs.readdirSync` with `fs.statSync` for each file causes a significant performance bottleneck due to excessive I/O operations. +**Action:** Always use `fs.readdirSync(..., { withFileTypes: true })` instead. It directly returns `fs.Dirent` objects containing type information, saving an I/O system call per file and dramatically speeding up directory traversals. diff --git a/src/files.ts b/src/files.ts index b1e1e43..06e1521 100644 --- a/src/files.ts +++ b/src/files.ts @@ -27,10 +27,9 @@ export function projectKey(start = process.cwd()): string | null { export function walkFiles(root: string, predicate: (path: string) => boolean): string[] { if (!existsSync(root)) return []; const files: string[] = []; - for (const entry of readdirSync(root)) { - const path = join(root, entry); - const stat = statSync(path); - if (stat.isDirectory()) files.push(...walkFiles(path, predicate)); + for (const entry of readdirSync(root, { withFileTypes: true })) { + const path = join(root, entry.name); + if (entry.isDirectory()) files.push(...walkFiles(path, predicate)); else if (predicate(path)) files.push(path); } return files.sort();