Skip to content
ByteStepStart with recursion

Big-O: what "grows faster" actually means

Free, no account

Big-O is usually taught as algebra: keep the dominant term, drop the constants, memorise a table of names. You can do all of that correctly and still have no idea whether a given piece of code is fast.

It is really about one question. When the input gets bigger, what happens to the work? Below, six algorithms answer that by being run. Each step feeds one of them an input twice the size of the last and counts the operations it actually performed. Nothing is plotted from a formula.

Start with linear and watch it to the end, then switch to binary. Same axis, same sizes, and a line that almost stops climbing. Then try pairs and exponential, and read the vertical scale before you decide the curves look similar.

1function scan(a, target) {
2 for (let i = 0; i < a.length; i++) {
3 if (a[i] === target) return i // one operation
4 }
5 return -1
6}

What it actually cost

every point is a real count, not a drawn curve

0125250375500266130192256operationsO(n)

Nothing measured yet. Each step runs the code on real inputs, a little larger each time, and counts the operations it actually performed.

1/26
Detail
26 steps

What a growth rate is, what one doubling costs, and how far apart the shapes really are.

The shape, not the number

An operation count on its own tells you nothing. Two hundred and fifty six operations is fast if the input was a million and slow if it was four. What matters is the relationship between the two, and that is what the notation names.

So read the chart as a shape rather than as heights. O(n) is a straight line: every item costs the same, so twice the items costs twice the work. O(log n) flattens, because each step throws away half of what is left and there are only so many halvings in any number. O(n²) bends upwards, because every new item has to meet every item already there.

What doubling does

The cleanest way to feel a growth rate is to stop looking at the total and start looking at what one doubling costs. Step the animation and read the caption each time.

  • Linear: doubling the input doubles the work. Steady, predictable, usually fine.
  • Logarithmic: doubling the input adds one operation. Not one per cent. One.
  • Quadratic: doubling the input roughly quadruples the work. This is the one that is fine in testing and fails in production.

All six, side by side

The player shows one shape at a time, because each one is measured by actually running it. Comparing them is a different job, so here they are together. This chart is drawn from the formulas rather than measured, which is why it can show all six at once.

O(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)input size →work
Drawn from the formulas, not measured, and each curve is scaled to its own maximum so that all six fit on one axis. Without that scaling the top two would press every other line flat against the bottom, which is the truth and tells you nothing. Read the bend, not the height.

Read the bend rather than the height. Flat means the input size does not matter. A line that rises and then levels off is throwing work away every step. A straight line is paying the same for every item. Anything that curves upwards is paying more for each item than for the one before, and that is the only group that eventually becomes unusable.

The same input, four costs

At 200 items the measurements are 8 operations, 200, 1,600 and 19,900. The same data, handed to four algorithms, and the slowest does about 100 times the work of the linear one and 2,488 times the work of the fastest.

That gap is why the notation exists. It is not about being precise about small inputs, where everything is fast and nobody cares. It is about knowing which of these you have written before the input gets large enough to find out the hard way.

Know someone stuck on this? Send it to them.

A classmate, a study group, someone learning this on their own at midnight. The link opens the lesson set up exactly as you have it, the same function and the same number, and runs from the first step so they watch the whole thing build rather than landing in the middle of it.

Also in this course

Binary search, and why it is easy to get wrong

Where the logarithmic line in this lesson comes from: a window that halves on every comparison.

Start there →