← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Memoization

Performance Intermediate
debt(d7/e5/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints note automated=no and tools are blackfire/xdebug — profilers that require deliberate instrumentation runs to spot repeated expensive calls. Misuse (memoizing side-effectful functions, wrong cache keys, unbounded growth) produces no compiler or linter warnings and won't surface until runtime profiling or user-visible bugs appear. Slightly better than d9 because profiling tools can expose the pattern if someone looks.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix describes a static-variable cache keyed by arguments — seemingly a small change — but common_mistakes reveal that fixing a badly memoized function (wrong cache key, side effects bypassed, unbounded growth, cross-request stale state) often requires rethinking which functions are pure, auditing all call sites for side effects, and potentially introducing cache-eviction strategies. This goes beyond a one-line patch but stays within component scope, landing at e5.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Memoization is typically applied per-function and its scope is contained within that function or class. It applies across web/cli/queue contexts but its structural weight is local — other parts of the codebase don't need to know about it. The risk of unbounded caches or cross-request static state adds a small ongoing tax, but it doesn't reshape the whole system.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The canonical misconception is treating memoization as equivalent to general caching — same concept, different scope and lifetime. Additionally, the common_mistakes list concrete, non-obvious pitfalls: memoizing functions with side effects silently skips those effects on repeat calls, and cross-request static properties without TTL cause stale data bugs. These are well-documented but regularly tripped over, placing this squarely at t5.

About DEBT scoring →

Also Known As

memoize function memoization result caching

TL;DR

Caching the result of a pure function call keyed by its arguments so repeated calls with the same inputs return immediately.

Explanation

Memoization is an optimisation for pure (or effectively pure) functions where the same inputs always produce the same output. The first call computes the result and stores it in a lookup table; subsequent calls with the same arguments return the cached result instantly. In PHP, this is commonly implemented with a local static array or instance property. It is most valuable for recursive computations (Fibonacci, tree traversals), repeated regex compilations, or expensive lookups (translations, configuration values) called multiple times per request.

Common Misconception

Memoization and caching are the same thing. Memoization is function-level — it caches the return value of a pure function keyed on its arguments, within a single execution context. General caching is broader, persists across requests, and applies to any data, not just function outputs.

Why It Matters

Memoization caches the result of a pure function call indexed by its arguments — calling an expensive function with the same input twice costs only one execution; subsequent calls return the cached result.

Common Mistakes

  • Memoizing functions with side effects — the cached result bypasses the side effect on repeat calls.
  • Not including all deterministic inputs in the cache key — different inputs produce the same key, returning wrong results.
  • Unbounded memoization caches — memory grows indefinitely for functions called with many unique inputs.
  • Memoizing across requests via static properties without TTL — stale results persist for the process lifetime.

Code Examples

✗ Vulnerable
function expensiveCalc(int $n): int {
  return // ... heavy computation each call
}
✓ Fixed
function expensiveCalc(int $n): int {
  static $cache = [];
  return $cache[$n] ??= /* heavy computation */;
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 60
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 2 pings T 2 pings F 0 pings S 2 pings S 1 ping M 1 ping T 3 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Ahrefs 11 Amazonbot 9 Scrapy 8 Perplexity 7 SEMrush 4 Google 3 Bing 3 ChatGPT 3 Unknown AI 2 Claude 1 Meta AI 1 Majestic 1
crawler 49 crawler_json 4
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Cache a function's return value in a static variable keyed by its arguments — PHP's static variables persist for the request lifetime, giving O(1) repeat calls with zero serialisation
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Expensive pure function called multiple times with same arguments within a request; recursive function with overlapping subproblems and no cache
Auto-detectable: ✗ No blackfire xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update


✓ schema.org compliant