Big O notation sounds academic, but its practical use is simple: it tells you how an algorithm’s cost grows as the input grows. You do not need a maths degree — you need to recognize a handful of patterns and what they mean when your data gets large.
What Big O actually measures
Big O describes the growth rate of an algorithm relative to its input size, ignoring constant factors and small terms. It does not tell you how many milliseconds something takes — it tells you how that time changes as the input doubles, triples, grows a thousandfold. The question it answers is: will this still work when the data is big?
The common classes, best to worst
- O(1) constant — the same cost regardless of input size. Looking up a value by key in a hash map.
- O(log n) logarithmic — cost grows slowly; halving the problem each step. Binary search.
- O(n) linear — cost grows in step with input. A single loop over the data.
- O(n log n) — the realm of good sorting algorithms.
- O(n^2) quadratic — a loop inside a loop. Fine for small inputs, painful for large ones.
- O(2^n) exponential — cost doubles with each added element. Only viable for tiny inputs.
Spotting it in your code
You can usually read complexity straight off the structure. A single loop over n items is O(n). A loop nested inside another loop over the same data is O(n^2). Repeatedly halving the search space is O(log n). A direct lookup with no loop is O(1). The nested loop is the one to watch — it is the most common accidental performance trap.
Why constants are dropped
Big O ignores constant factors because, as input grows, growth rate dominates everything else. An O(n) algorithm that does a little extra work per item still beats an O(n^2) one once n is large enough. That said, for small, fixed inputs the constants can matter more than the class — Big O is about scale, not micro-optimization.
- Big O measures how cost grows with input size, not absolute speed.
- Know the ladder: O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n).
- A loop inside a loop is O(n^2) — the most common scaling trap.
- It is about behaviour at scale; for tiny inputs, constants can matter more.
Frequently asked questions
Is lower Big O always better?
Usually at scale, but not always for small inputs, where a simpler O(n^2) algorithm may beat a complex O(n log n) one due to lower constant overhead.
What about space complexity?
Big O also describes memory use, expressed the same way. An algorithm can trade time for space — using more memory to run faster, or vice versa.