{
    "slug": "time_complexity",
    "term": "Time Complexity (Big O)",
    "category": "performance",
    "difficulty": "intermediate",
    "short": "A notation describing how an algorithm's execution time grows relative to input size — O(1), O(n), O(n log n), O(n²)…",
    "long": "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.",
    "aliases": [
        "Big O notation",
        "algorithmic complexity",
        "O(n) complexity"
    ],
    "tags": [
        "performance",
        "algorithms",
        "fundamentals"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "space_complexity",
        "n_plus_one",
        "database_indexing"
    ],
    "prerequisites": [
        "big_o_notation",
        "space_complexity",
        "algorithms"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Big_O_notation"
    ],
    "bad_code": "// O(n²) — nested loop over the same array\n$hasDuplicates = false;\nfor ($i = 0; $i < count($arr); $i++) {\n    for ($j = $i + 1; $j < count($arr); $j++) {\n        if ($arr[$i] === $arr[$j]) { $hasDuplicates = true; break 2; }\n    }\n}",
    "good_code": "// O(n) — hash map lookup\n$seen = [];\n$hasDuplicates = false;\nforeach ($arr as $v) {\n    if (isset($seen[$v])) { $hasDuplicates = true; break; }\n    $seen[$v] = true;\n}\n\n// Or one-liner:\n$hasDuplicates = count($arr) !== count(array_unique($arr));",
    "example_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).",
    "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",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/time_complexity",
        "html_url": "https://codeclaritylab.com/glossary/time_complexity",
        "json_url": "https://codeclaritylab.com/glossary/time_complexity.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Time Complexity (Big O)](https://codeclaritylab.com/glossary/time_complexity) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/time_complexity"
            }
        }
    }
}