<!--
title: Validate before expensive operations
domain: devtools
topic: Performance Optimization
language: TypeScript
source: microsoft/vscode
updated: 2025-07-10
url: https://awesomereviewers.com/reviewers/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.

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:
```typescript
async function getWorktrees(): Promise<Worktree[]> {
    // Spawns process on hot path
    const result = await spawn('git', ['worktree', 'list']);
    return parseWorktrees(result);
}
```

After:
```typescript
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);
}
```
