Perform lightweight validation checks before executing expensive operations to avoid unnecessary resource consumption. This is especially important in hot paths or frequently called functions.
Key practices:
Example - Before:
async function getWorktrees(): Promise<Worktree[]> {
// Spawns process on hot path
const result = await spawn('git', ['worktree', 'list']);
return parseWorktrees(result);
}
After:
async function getWorktrees(): Promise<Worktree[]> {
// Use fs operations first
const hasWorktrees = await fs.exists('.git/worktrees');
if (!hasWorktrees) {
return [];
}
// Only spawn process if needed
const result = await spawn('git', ['worktree', 'list']);
return parseWorktrees(result);
}
Enter the URL of a public GitHub repository