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

Time Complexity (Big O)

Performance Intermediate
debt(d7/e5/b5/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 indicate automated detection is 'no', and while tools like blackfire, phpstan, and phpbench can help profile or benchmark, they don't automatically flag algorithmic complexity issues — a developer must interpret profiling results or manually review code patterns like nested loops and in_array inside loops. There is no compiler or linter that reliably catches O(n²) vs O(n) choices.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix advises profiling first, but common_mistakes show the actual remediation — replacing nested loops with hash maps, flipping arrays for O(1) lookups, moving sorts outside loops — requires understanding data flow and may touch multiple functions or files. It's more than a one-line swap but less than a cross-cutting architectural change.

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

Closest to 'persistent productivity tax' (b5). The applies_to covers both web and cli contexts, meaning poor algorithmic choices can affect multiple code paths. The concept imposes an ongoing cognitive load on maintainers — every future change to data-processing code must consider input size growth — but it doesn't define the entire system's shape or impose a strong gravitational pull across all layers.

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

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states the trap: developers believe O(n²) is always too slow for production, when in fact the practical impact depends entirely on n. This is a well-documented gotcha — premature optimisation is a classic mistake — but it doesn't contradict how similar concepts work elsewhere or reliably cause catastrophic misuse.

About DEBT scoring →

Also Known As

Big O notation algorithmic complexity O(n) complexity

TL;DR

A notation describing how an algorithm's execution time grows relative to input size — O(1), O(n), O(n log n), O(n²)…

Explanation

Big O notation characterises the worst-case growth rate of an algorithm. O(1) constant time (hash table lookup), O(log n) logarithmic (binary search), O(n) linear (single pass), O(n log n) linearithmic (merge sort), O(n²) quadratic (nested loops), O(2ⁿ) exponential (naive recursive Fibonacci). PHP developers commonly introduce O(n²) accidentally by nesting foreach loops over large arrays or running queries inside loops. Understanding complexity guides correct data structure choice and prevents scalability cliffs.

Common Misconception

O(n²) algorithms are always too slow for production. The practical impact depends on n — O(n²) over 100 items is negligible; over 100,000 items it may be catastrophic. Measure actual input sizes before optimising — premature complexity reduction often reduces readability for no real gain.

Why It Matters

Time complexity predicts how an algorithm scales — an O(n²) solution that takes 1ms for 100 items takes 100 seconds for 100,000, making the choice of algorithm critical for any data that can grow.

Common Mistakes

  • Nested loops over the same data — O(n²) that could be O(n) with a hash map.
  • Using in_array() in a loop — O(n) per call makes the loop O(n²); flip the array for O(1) lookups.
  • Sorting inside a loop — sort() is O(n log n); sorting once before the loop is correct.
  • Not considering input size growth — code that's fast on dev data can be catastrophically slow on production volumes.

Code Examples

💡 Note
Replacing O(n²) with O(n) using a hash map is one of the most common and impactful optimisations. array_key_exists / isset on PHP arrays is O(1).
✗ Vulnerable
// O(n²) — nested loop over the same array
$hasDuplicates = false;
for ($i = 0; $i < count($arr); $i++) {
    for ($j = $i + 1; $j < count($arr); $j++) {
        if ($arr[$i] === $arr[$j]) { $hasDuplicates = true; break 2; }
    }
}
✓ Fixed
// O(n) — hash map lookup
$seen = [];
$hasDuplicates = false;
foreach ($arr as $v) {
    if (isset($seen[$v])) { $hasDuplicates = true; break; }
    $seen[$v] = true;
}

// Or one-liner:
$hasDuplicates = count($arr) !== count(array_unique($arr));

Added 15 Mar 2026
Edited 22 Mar 2026
Views 55
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 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 4 pings S 1 ping S 1 ping M 0 pings T 1 ping W 3 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings 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
Perplexity 9 Amazonbot 9 Scrapy 7 SEMrush 5 Ahrefs 4 Unknown AI 3 Google 3 Claude 2 ChatGPT 2 Meta AI 1 PetalBot 1
crawler 42 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Profile before optimising — O(n²) on 100 items is 10,000 operations, fast; O(n²) on 100,000 items is 10 billion, catastrophic; measure actual execution time before rewriting algorithms
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Nested loops O(n²) on potentially large datasets; sorting array every iteration O(n² log n); in_array inside loop O(n²) instead of hash lookup O(n)
Auto-detectable: ✗ No blackfire phpstan phpbench
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Function Tests: Update


✓ schema.org compliant