Big-O complexity explorer
easyPlot every complexity class on one chart and step the input size up. The point is not the formulas - it is the moment O(2ⁿ) leaves the screen and O(n²) starts costing hours.
Saved in this browser - no sign-up, nothing sent anywhere.
How big-o complexity explorer works
Big-O answers one question: when the input grows, how fast does the work grow? An O(n) scan of a million elements is a million operations - a few milliseconds. An O(n²) pair-check on the same input is a trillion, roughly 17 minutes at a billion operations per second. Same data, same machine; the only difference is the shape of the loop.
The chart plots six classes - O(log n), O(n), O(n log n), O(n²), O(n³) and O(2ⁿ) - and steps n upward while a chip row translates each curve into wall-clock time at a billion operations per second. At n = 8 the curves are bunched together; by n = 36 the exponential one needs 68 billion operations - about a minute - while everything polynomial is still too cheap to measure.
The practical skill is reading loop shapes, not memorising formulas. One pass over the data is n. A nested pass over pairs is n². A counter that doubles is log n. A split into two halves that each do linear work is n log n. Most complexity analysis in interviews is recognising those four patterns in code.
Step by step
- Six curves - O(log n) up to O(2ⁿ) - start at n = 2 and grow together as the timeline steps the input size toward 64.
- Through n = 8 the curves bunch at the bottom. Every algorithm looks fine on inputs this small, which is exactly why small tests hide bad complexity.
- By n = 20 the chips split: every polynomial class still reads instant or microseconds, while O(2ⁿ) crosses a million operations - about a millisecond.
- At n = 36, O(2ⁿ) is 68 billion operations - roughly a minute of compute - while O(n²) sits at 1,296, far too cheap to measure.
- The final frame at n = 64: O(n log n) needs 384 operations, O(n²) needs 4,096, and O(2ⁿ) needs a 20-digit number of them.
- Replay with the logarithmic y-axis off: the exponential curve flattens all five others into the floor, which is why the log scale is the default.
Reference implementation
Python
# O(1) - the input size does not appear
def first(a):
return a[0]
# O(log n) - the range halves each step
def binary_search(a, x):
lo, hi = 0, len(a) - 1
while lo <= hi:
mid = (lo + hi) // 2
...
# O(n) - one visit per element
def total(a):
return sum(a)
# O(n^2) - every pair
def has_duplicate(a):
for i in range(len(a)):
for j in range(i + 1, len(a)):
if a[i] == a[j]:
return True
return False
# O(2^n) - every subset
def subsets(a):
if not a:
return [[]]
rest = subsets(a[1:])
return rest + [[a[0]] + s for s in rest]JavaScript
// O(1)
const first = (a) => a[0];
// O(n)
const total = (a) => a.reduce((s, x) => s + x, 0);
// O(n^2) - every pair
function hasDuplicate(a) {
for (let i = 0; i < a.length; i++)
for (let j = i + 1; j < a.length; j++)
if (a[i] === a[j]) return true;
return false;
}
// O(n) with O(n) memory - the classic trade
const hasDuplicateFast = (a) => new Set(a).size !== a.length;Worth noticing
Constants stop mattering, and that is the point
An O(n²) algorithm that is 100× faster per operation still loses to O(n log n) somewhere - and once it does, it loses by more every time the input grows. Big-O describes the slope, not the intercept.
The wall between n² and 2ⁿ
Step the timeline to n = 40. Quadratic is 1,600 operations - instant. Exponential is a trillion - about 18 minutes. At n = 60 the exponential is older than the universe. This is the boundary between 'slow' and 'impossible'.
How to read a loop
A single pass is n. A nested pass is n². A loop whose counter doubles is log n. A recursive split into two halves that each do linear work is n log n. Most complexity analysis is just recognising those four shapes.
Common pitfalls
- Benchmarking on small inputs. At n = 8 all six curves are within a few hundred operations of each other; the quadratic disaster only appears at the scale you did not test.
- Treating big-O as a speed measurement. It describes the slope, not the intercept - an O(n²) routine with a tiny constant genuinely beats O(n log n) on small inputs, then loses by more every time n grows.
- Expecting hardware to rescue an exponential algorithm. A machine twice as fast moves the O(2ⁿ) wall from n = 40 to n = 41 - one extra element per doubling.
- Adding complexities when they should multiply, or the reverse. Two sequential loops are O(n), not O(n²); a loop nested inside another multiplies.
Where it is used
- Choosing a data structure before writing code - the difference between shipping and rewriting at 10× the data.
- The follow-up in nearly every coding interview: state the time and space complexity of what you just wrote.
- Capacity planning: predicting whether tonight's batch job survives next year's dataset without renting a bigger machine.
- Hunting accidental O(n²) in production - string concatenation in a loop, repeated array unshift, nested lookups over the same list.
Frequently asked questions
How long do the common complexity classes actually take to run?
At a billion operations per second - the usual back-of-envelope figure - O(n log n) on a million elements is about 20 million operations, done in 20 milliseconds. O(n²) on the same input is a trillion operations, roughly 17 minutes. O(2ⁿ) is about a millisecond at n = 20 and 18 minutes by n = 40.
Does a better big-O always mean faster code?
Only past the crossover point. Big-O ignores constant factors, so insertion sort beats merge sort below a few dozen elements - which is why production sorts switch to it for small runs. But the crossover always exists, and beyond it the better class wins by a wider margin every time the input grows.
How do I work out the complexity of my own code?
Recognise four shapes. A single pass over the data is O(n). A nested pass over pairs is O(n²). A loop whose counter doubles or halves each turn is O(log n). A recursion that splits the input in half and does linear work per level is O(n log n). Multiply nested shapes, add sequential ones.
Why does the chart use a logarithmic y-axis?
Because the classes are separated by multiplication, not addition. On a linear axis O(2ⁿ) dwarfs everything by n = 30 and the other five curves flatten into the floor. A log axis turns each class into a distinct slope, so the whole family stays readable at once.