Big O Complexity

How much slower does a program get when you give it more to do? See the curves, read the code, and find out.

What is Big O?

Big O is a way to describe how much slower a computer program gets when you give it more stuff to work on. The letter n means “how many items there are.” So O(n) means the work grows at the same rate as the number of items, and O(n²) means it grows like n × n.

Big O doesn't care about exact seconds. It only cares about the shape of the growth. When n gets big, the shape is what matters most.

Try it: click the buttons below to add curves to the graph. Hover over the graph to see the numbers, and read the code to see what each one looks like in a real program.

06561.3k2.0k2.6k11121304050n (input size)operationsO(n)O(n²)
Pick an n:10

Quadratic: a loop inside a loop. Double the input and you get 4 times the work. Like every kid in class shaking hands with every other kid.

// O(n²) — bubble sort
function bubbleSort(arr: number[]): number[] {
  const a = [...arr];
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < a.length - i - 1; j++) {
      if (a[j] > a[j + 1]) {
        [a[j], a[j + 1]] = [a[j + 1], a[j]];
      }
    }
  }
  return a;
}

// Two nested loops: every pair of elements
// is compared. n=1000 → ~500,000 comparisons.

What If Computers Got Faster?

Say a program can handle n = 10 items in one second. If we got a computer 1,000 times faster, how many items could it handle in that same second? Notice how a super-fast computer barely helps the slow ones at the bottom.

Big OSteps at n=101,000× faster computer1,000,000× faster computer
O(1)1∞∞
O(log n)3∞∞
O(n)1010k10.0M
O(n log n)333k1.6M
O(n²)10031610k
O(n³)1.0k1001k
O(2ⁿ)1.0k2030
O(n!)3.6M1215
Click a row to show it on the graph and see its code. A number like 10^301 means a 1 with 301 zeros after it. ∞ means the number is too big to even show. (For O(1), there's truly no limit: it does the same work no matter how big n is.)