Back to all reviewers

Validate before expensive operations

microsoft/vscode
Based on 4 comments
Typescript

Perform lightweight validation checks before executing expensive operations to avoid unnecessary resource consumption. This is especially important in hot paths or frequently called functions.

Performance Optimization Typescript

Reviewer Prompt

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:

  1. Use file system operations instead of process spawning
  2. Check file sizes before reading entire files
  3. Filter collections before heavy processing
  4. 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);
}
4
Comments Analyzed
Typescript
Primary Language
Performance Optimization
Category

Source Discussions