domains / / microsoft/vscode
Validate before expensive operations
Perform lightweight validation checks before executing expensive operations to avoid unnecessary resource consumption. This is especially important in hot paths or frequently called functions.
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:
- Use file system operations instead of process spawning
- Check file sizes before reading entire files
- Filter collections before heavy processing
- Use specific API methods instead of broad queries
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);
}