Files
awesome-reviewers/_reviewers/typescript-cache-expensive-computed-values.md
2025-08-03 22:10:18 +03:00

1.5 KiB

title, description, repository, label, language, comments_count, repository_stars
title description repository label language comments_count repository_stars
Cache expensive computed values Cache frequently accessed or computed values to avoid redundant calculations and property lookups. This applies to: 1. Loop invariant values 2. Expensive computations microsoft/typescript Performance Optimization TypeScript 4 105378

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.