Big O notation describes how an algorithm's workload grows as the input grows. It answers one question: if the input gets 10× bigger, how much more work happens? An O(n) algorithm does 10× the work. An O(n²) algorithm does 100×. An O(log n) algorithm does barely any extra work at all. That growth rate, not the exact operation count, decides whether your code survives real-world data sizes.
Big O deliberately ignores constants and small terms. An algorithm doing 3n + 15 operations is written O(n), because as n grows, the "3" and "+15" stop mattering. What matters is the shape of the curve: linear, logarithmic, quadratic, exponential. Two O(n) algorithms may differ in constant speed by a lot, but an O(n) algorithm will always beat an O(n²) one eventually, that crossover is what Big O captures.
The complexity classes you'll meet constantly
- O(1), constant: array index access, hash table lookup (average), stack push/pop
- O(log n), logarithmic: binary search, balanced tree operations; halving each step
- O(n), linear: scanning an array once, linear search
- O(n log n), linearithmic: merge sort, quick sort average case; the practical floor for comparison sorting
- O(n²), quadratic: nested loops over the data; bubble/selection/insertion sort worst cases
- O(2ⁿ), exponential: trying every subset; explodes fast, fine only for tiny n
- O(n!), factorial: trying every ordering; even n = 15 is billions of steps
Learn to see the shape in code
// O(1), work doesn't depend on n
const first = arr[0];
// O(n), touches each element once
for (const item of arr) { console.log(item); }
// O(n²), for every element, scan all elements
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (arr[i] === arr[j]) count++;
}
}
// O(log n), the search space halves each loop
let low = 0, high = n - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
// ... compare and discard half ...
}
// O(2ⁿ), every call branches into two calls
function fib(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}Best, average, and worst case
The same algorithm can behave very differently on different inputs. Linear search is O(1) in the best case (target is first), O(n) in the worst case (target is last or absent), and O(n/2) ≈ O(n) on average. Big O by convention usually quotes the worst case, because it is a guarantee: "this will never be slower than this."
Some algorithms have dramatic gaps between average and worst case. Quick sort averages O(n log n) but degrades to O(n²) with consistently bad pivot choices. When you read complexity tables, check which case is being quoted, and remember that a bad worst case matters most when inputs may be adversarial, while the average case predicts day-to-day feel.
Space complexity: the other half of the bill
Time is not the only resource. Space complexity measures how much extra memory an algorithm needs as input grows. Sorting in place with swaps uses O(1) extra space; merge sort needs a temporary buffer the size of the input, O(n) space. Recursion also consumes space: each pending call lives on the call stack, so a recursion n levels deep costs O(n) space even if it looks "free" in code.
Time and space often trade against each other. Memoization (caching computed results) spends O(n) memory to cut repeated O(2ⁿ) work down to O(n) time. Prefix sums spend O(n) memory once to answer many range-sum queries in O(1) each. When you optimize, ask: am I buying speed with memory, or memory with speed, and is that the right trade for this system?
How big differences get at scale (rough operation counts)
| Operation / case | Cost | Notes |
|---|---|---|
| O(log n), n = 1,000,000 | ~20 steps | instant feel |
| O(n), n = 1,000,000 | 1,000,000 steps | fine for one pass |
| O(n log n), n = 1,000,000 | ~20,000,000 steps | sorting territory |
| O(n²), n = 1,000,000 | 1,000,000,000,000 steps | minutes-to-hours; avoid |
Exact constants vary by machine, Big O predicts the growth trend, not exact milliseconds. Never trust "it's fast on my sample data" alone.
Practice
What is the time complexity of this function?
function pairs(arr) {
const n = arr.length;
const out = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
out.push([arr[i], arr[j]]);
}
}
return out;
}Reveal explanation
The outer loop runs n times and the inner loop also runs n times for every outer pass, so the body executes n × n = n² times → O(n²).