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

Time Complexity (Big O)

performance Intermediate

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 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Perplexity 9 Amazonbot 7 Unknown AI 3 SEMrush 3 Ahrefs 2 Google 2
crawler 25 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