
Big O answers one concrete question: how does the work grow when the input grows? It does not tell you how many milliseconds a function will take, and it does not replace a benchmark. It describes the shape of the curve before real volume turns a small engineering choice into a product problem.
If a search takes twice as much work when the data doubles, it behaves differently from one whose cost barely changes. That distinction matters more than an isolated measurement on your laptop.
Big O does not measure speed. It measures how the cost worsens as
ngrows.
01. Big O in 30 Seconds
The letter n represents input size: users, rows, nodes, characters, files, pixels, or events. The notation summarizes how many operations—or how much memory—a solution needs as n increases.
Six visual Big O growth patterns
| Complexity | Mental image | Common example |
|---|---|---|
O(1) | one direct jump | read by index or key |
O(log n) | discard half | binary search |
O(n) | scan once | search a list |
O(n log n) | divide and combine | efficient sorting |
O(n²) | compare pairs | nested loops |
O(2ⁿ) | explore combinations | combinatorial brute force |
These curves do not say whether code is good or bad. They say how sensitive it is to growth. With small inputs almost every approach can feel fast; the separation appears when n stops being test data and becomes real traffic, content, or users.
02. Scale Changes the Decision
Imagine, only for comparison, that every operation costs one unit:
| Complexity | n = 10 | n = 1,000 | n = 1,000,000 |
|---|---|---|---|
O(1) | 1 | 1 | 1 |
O(log n) | 4 | 10 | 20 |
O(n) | 10 | 1,000 | 1,000,000 |
O(n log n) | 40 | 10,000 | 20,000,000 |
O(n²) | 100 | 1,000,000 | 1,000,000,000,000 |
Operation growth compared across different input sizes
This is not an exact benchmark: it ignores constants, hardware, and implementation details. It is a magnifying glass. It explains why two solutions that look identical with ten items can separate brutally with one million.
The practical rule is simple: do not optimize by instinct, but do not ignore the growth shape either. Identify the curve first; then measure the real implementation.
03. Three Decisions That Change the Curve
Suppose you repeatedly need to answer whether a user exists. Scanning the array is clear and costs O(n) per query. Building a Set costs O(n) once, uses extra memory, and enables average O(1) lookups.
function includesUser(users: { id: string }[], id: string) {
return users.some((user) => user.id === id); // O(n)
}
const userIds = new Set(users.map((user) => user.id)); // O(n)
const exists = userIds.has(id); // average O(1)There is no universal winner. For one query over ten items, the array is probably better: less code and no extra structure. For thousands of queries over the same set, paying for the index once can change the total cost.
If the data is already sorted, binary search offers another path: every step removes half of the remaining space.
function binarySearch(values: number[], target: number) {
let low = 0;
let high = values.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (values[mid] === target) return mid;
if (values[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}With one million items it needs roughly twenty divisions, but it requires an invariant: the collection must be sorted. Complexity never appears alone; it comes with conditions, memory, and preparation costs.
Visual comparison of linear scan, binary search, and Map or Set lookup
04. Where the Danger Starts
O(n log n) is a common boundary for sorting general data by comparison. Merge Sort divides and combines; Quick Sort partitions; Heap Sort uses a priority structure. The curve grows faster than linear, but far slower than quadratic.
The dangerous jump appears when every item scans the collection again. A double loop often produces O(n²):
function hasDuplicate(values: string[]) {
for (let i = 0; i < values.length; i += 1) {
for (let j = i + 1; j < values.length; j += 1) {
if (values[i] === values[j]) return true;
}
}
return false;
}Not every nested loop is automatically quadratic: what matters is how often each level actually runs. But when both traverse n, doubling the input can quadruple the work. An index, hashing, or spatial partition can change the curve.
O(2ⁿ) is another category. It appears when exploring subsets or accumulated binary choices. At n = 50, trying every combination stops being an optimization task and becomes an impossible problem. The usual tools are memoization, pruning, heuristics, and explicit product limits.
Contrast between pair comparison O(n²) and combinatorial explosion O(2ⁿ)
05. What Big O Does Not Tell You
Big O removes details to reveal a trend. That simplification is useful, but it has limits:
- Constants: an
O(1)operation can have a high fixed cost. - Average and worst case: a hash table usually queries in average
O(1), not as an absolute guarantee. - Memory: faster lookups with a
MaporSetconsume additional space. - Data distribution: Quick Sort depends on its partitions; a tree depends on its balance.
- Hardware and cache: two solutions with the same complexity can behave differently in production.
That is why Big O and profiling do not compete. Big O helps you detect scaling risk before building; a profiler tells you where real time is spent after building.
06. Decision Checklist
Before changing a solution, answer:
- What does
nrepresent, and how large can it become? - Does this operation happen once or thousands of times?
- Must you prepare or sort the data before querying it?
- Can you trade memory for faster repeated lookups?
- Does the worst case matter for UX, security, or infrastructure?
- Does the problem appear in real measurements or only in theory?
Prefer clarity when the data is small and the path is not critical. Change the structure when the same query repeats. Review nested loops when n depends on users. Bound combinatorial problems before trying to micro-optimize them.
Learning Big O is not about memorizing formulas. It is about recognizing six shapes and asking one question before the system grows: what cost am I buying with this decision?