Back to all reviewers

Cache expensive computed values

microsoft/typescript
Based on 4 comments
Typescript

Cache frequently accessed or computed values to avoid redundant calculations and property lookups. This applies to: 1. Loop invariant values 2. Expensive computations

Performance Optimization Typescript

Reviewer Prompt

Cache frequently accessed or computed values to avoid redundant calculations and property lookups. This applies to:

  1. Loop invariant values
  2. Expensive computations
  3. Repeated property access
  4. Function call results

Example - Before:

while (index < path.length) {
    // path.length accessed on every iteration
    if (path.indexOf(separator, index) !== -1) {
        // indexOf called multiple times
    }
}

Example - After:

const pathLength = path.length; // Cache loop invariant
const separatorIndex = path.indexOf(separator, index); // Cache computation
while (index < pathLength) {
    if (separatorIndex !== -1) {
        // Use cached values
    }
}

This optimization is particularly important in performance-critical paths and loops. When implementing, consider:

  • Caching loop invariants outside the loop
  • Storing repeated computation results in local variables
  • Caching property accesses that won’t change during execution
  • Using local variables for values accessed multiple times

The performance impact can be significant, especially when the cached values are used frequently or the computations are expensive.

4
Comments Analyzed
Typescript
Primary Language
Performance Optimization
Category

Source Discussions