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

Premature Optimisation

Code Quality Beginner
debt(d7/e5/b5/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). detection_hints.automated is 'no' — profilers like blackfire/xdebug can reveal that optimisations aren't on hot paths, but identifying premature optimisation requires human judgement in code review.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says write readable code first then profile, but unwinding existing premature optimisations (caches, contorted algorithms, avoided abstractions) typically means refactoring multiple sites where the over-optimisation has spread.

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

Closest to 'persistent productivity tax' (b5). applies_to spans web/cli/queue contexts; premature optimisations sprinkled through code (avoided OOP, complex caching, micro-optimised loops) slow down every future maintainer who has to decode why the code is shaped oddly.

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

Closest to 'serious trap' (t7). The misconception is exactly that 'premature optimisation' means never think about performance — leading developers to either over-optimise everything or swing the other way and ignore algorithmic complexity entirely. The phrase contradicts its actual meaning for most who haven't read Knuth.

About DEBT scoring →

Also Known As

early optimisation premature optimization Knuth rule

TL;DR

Optimising code before measuring where the actual bottleneck is — trading readability for performance gains that often don't matter.

Explanation

Donald Knuth's famous observation — 'premature optimisation is the root of all evil' — warns against sacrificing code clarity for performance improvements that are speculative or negligible. Most performance problems lie in a small fraction of the code; optimising the wrong parts wastes time and produces harder-to-maintain code. The correct approach: write clear, correct code first; measure with a profiler to find real bottlenecks; optimise only what the data confirms is slow. In PHP, micro-optimisations (avoiding function calls, hand-rolling loops) almost never matter — I/O and query patterns almost always do.

Common Misconception

Premature optimisation means you should never think about performance until the end. It means not optimising before you have measured — designing for reasonable algorithmic complexity from the start is not premature optimisation, it is good engineering.

Why It Matters

Optimising before profiling makes code harder to read and maintain in exchange for performance improvements that are often unmeasurable — the bottleneck is almost never where developers intuit it is.

Common Mistakes

  • Micro-optimising loop constructs (for vs foreach vs while) without benchmarking actual impact.
  • Using static methods or avoiding OOP for 'performance' in application code where the overhead is negligible.
  • Caching results before measuring whether the operation is actually slow.
  • Choosing a complex algorithm over a simple one for scale that the application will never reach.

Code Examples

💡 Note
"Premature optimisation is the root of all evil" — Knuth. Profile first, then optimise the measured bottleneck.
✗ Vulnerable
// Micro-optimised before profiling — complex and wrong
public function getActiveUsers(): array {
    // Avoid array_filter — 'too slow'
    $result = [];
    $users  = $this->users;
    $count  = count($users); // cache count 'for speed'
    for ($i = 0; $i < $count; $i++) {
        if ($users[$i]->active) $result[] = $users[$i];
    }
    return $result;
}
✓ Fixed
// Write the clear version first — profile before optimising
public function getActiveUsers(): array {
    return array_values(array_filter($this->users, fn($u) => $u->active));
}

// If this is actually slow (profiler shows it) — fix the query, not the PHP:
// SELECT * FROM users WHERE active = 1  (let the DB filter, not PHP)

Added 15 Mar 2026
Edited 22 Mar 2026
Views 62
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 4 pings F 1 ping S 2 pings S 0 pings M 0 pings T 4 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 11 Amazonbot 9 SEMrush 5 Perplexity 4 Ahrefs 4 ChatGPT 4 Unknown AI 3 Google 3 Majestic 2 Claude 2 Meta AI 1 Bing 1 PetalBot 1
crawler 45 crawler_json 4 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Write readable correct code first; profile to find actual bottlenecks; optimise only the hot paths that profiling identifies — the 3% that matters
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Micro-optimisations (isset vs array_key_exists) without profiling data; complex caching of rarely-called functions; bitwise operations for simple booleans
Auto-detectable: ✗ No blackfire xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update


✓ schema.org compliant