Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-06-05 - Avoid statSync in filesystem traversal
**Learning:** In local-first CLI applications that heavily rely on file system traversal, using `readdirSync` followed by `statSync` creates a severe performance bottleneck due to excessive I/O operations.
**Action:** Always use `readdirSync(path, { withFileTypes: true })` to retrieve `fs.Dirent` objects which provide `.isDirectory()` without additional I/O overhead.
11 changes: 6 additions & 5 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { basename, dirname, join, relative, resolve } from "node:path";

export type VspecConfig = { vspec_format: 1; key_prefix: string; spec_language?: "ko" | "en" | "match-input" };
Expand Down Expand Up @@ -27,10 +27,11 @@ 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));
// ⚡ Bolt: Use withFileTypes to avoid separate statSync calls, which significantly reduces I/O overhead
// when traversing large directories.
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();
Expand Down