Big O Notation — Data Structures

Big O is a way of writing how fast the number of steps in a piece of code grows as the amount of data (n) grows. It usually counts the worst case, when the…

Code that checks a sign-up list for duplicate names runs smoothly when it is tested on 10 names. When it is used on 100,000 names, the page freezes. The computer is the same and the code is the same. Only the amount of data changed.

With Big O, you can predict this kind of freeze before it happens. Big O is a way of writing how fast the number of steps in a piece of code grows as the data grows. The amount of data is called n, for example the length of an array. You count steps (often also called operations), not seconds, because seconds depend on the computer, while the number of steps is the same on any computer.

Count the steps

Here are three functions that work on an array of n items:

This table shows the largest number of steps each function takes, in the worst case: the value isn't there and there are no duplicates, so every item has to be checked.

n getFirst findItem hasDuplicate
10 1 10 45
100 1 100 4,950
1,000 1 1,000 499,500

This is the code of the three functions:

function getFirst(arr) {
  return arr[0]; // 1 step, however long arr is
}

function findItem(arr, target) {
  for (let i = 0; i < arr.length; i++) { // at most n passes
    if (arr[i] === target) return i;
  }
  return -1;
}

function hasDuplicate(arr) {
  for (let i = 0; i < arr.length; i++) {       // n passes
    for (let j = i + 1; j < arr.length; j++) { // each pass loops again
      if (arr[i] === arr[j]) return true;
    }
  }
  return false;
}

console.log(getFirst([4, 8, 15]));     // 4
console.log(findItem([4, 8, 15], 15)); // 2
console.log(hasDuplicate([4, 8, 15])); // false

Why 45 steps for 10 items, and not 10 × 10 = 100? j (the counter of the inner loop) starts at i + 1, so each pair is compared only once: about half of n × n. Big O drops fixed multipliers (fixed numbers to multiply by) like that "half", because with or without it, ten times the data still means roughly a hundred times the steps. When both loops run the full n times, the code takes exactly n × n steps, and that's O(n²) too.

Six classes you'll meet often

These six classes (groups) of Big O go from the fewest steps to the most, with the rough number of steps for n = 1,000:

Big O Name Example Steps (n = 1,000)
O(1) constant take arr[0] 1
O(log n) logarithmic binary search about 10
O(n) linear one loop 1,000
O(n log n) linearithmic fast sorting, like merge sort about 10,000
O(n²) quadratic a loop inside a loop 1,000,000
O(2ⁿ) exponential trying every pick or skip combination a 302 digit number

log n means how many times n can be halved until 1 is left. One example of O(log n) code is binary search, a way to search data that is already sorted. At every step, binary search throws away half of the data that is left. It works like finding a word in a dictionary: open it in the middle, drop the half that can't hold the word, then repeat. 1,000 items take about 10 steps, and 1,000,000 items only about 20. So for big data, O(log n) is far faster than O(n) and O(n log n), and even more so than O(n²).

A quick way to guess Big O

⚠️ Common ways to misread Big O

TL;DR: Big O is a way of writing how fast the number of steps in a piece of code grows as the amount of data (n) grows. It usually counts the worst case, when the code takes the most steps. Code with no loop, like arr[0], is O(1): the steps stay the same. One loop over all the data is O(n): twice the data, twice the steps. A loop inside a loop is O(n²), typed O(n^2): ten times the data, about a hundred times the steps. Code that halves its data at every step is O(log n): about 20 steps for a million items. So for big data, O(log n) is the fastest among O(n²), O(n) and O(n log n) (fast sorting, for example). Fixed multipliers like 2 are dropped, so 2n steps is still written O(n).

⚡ No loop: O(1)

Write:

function getFirst(arr) {
  return arr[0]; // 1 step, however long arr is
}

Meaning: Big O is a way of writing how fast the number of steps in code grows as the amount of data (n) grows. getFirst has no loop, so it always takes 1 step: O(1). Reading an array item by index, like arr[i], is always O(1).

⚡ One loop: O(n)

Write:

function findItem(arr, target) {
  for (let i = 0; i < arr.length; i++) { // at most n passes
    if (arr[i] === target) return i;
  }
  return -1;
}

Meaning: findItem checks the items of arr one by one. With ten times the data, it takes ten times the steps: O(n). Big O usually counts the worst case, when the value isn't there.

⚡ A loop inside a loop: O(n²)

Guess first: hasDuplicate looks for duplicate items with a loop inside a loop. It takes 45 steps for 10 items. How many for 100 items?

4,950 steps. With ten times the data, it takes roughly a hundred times the steps, because every pass of the outer loop runs another loop inside it.

Meaning: A loop inside a loop is O(n²), typed O(n^2). 45 is only about half of 10 × 10, because each pair of items is compared only once. Big O drops fixed multipliers like this "half": ten times the data still means roughly a hundred times the steps.

⚡ Halved at every step: O(log n)

Guess first: binary search looks through sorted data, and at every step it throws away half of the data that is left. How many steps does it take on a million items?

Only about 20. A million items can be halved only about 20 times until 1 is left.

Meaning: Code that halves its data at every step is written O(log n): log n is how many times n can be halved until 1 is left. From the fastest for big data: O(1), O(log n), O(n), O(n log n) (fast sorting, for example), then O(n²).

What you will learn