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:
getFirst(arr): takes the first item.findItem(arr, target): finds one value by checking the array's items one by one.hasDuplicate(arr): checks whether any item appears twice, by comparing every pair of 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
getFirsthas no loop, so it always takes 1 step, however long the array is. That's O(1), read "O of one", or constant.findItemhas one loop. With ten times the data, it takes ten times the steps. That's O(n), or linear. Sometimes the value is found early, but Big O usually counts the worst case.hasDuplicatehas a loop inside a loop (a nested loop): each item is compared with every item after it. With ten times the data, it takes roughly a hundred times the steps. That's O(n²), read "O of n squared". On a keyboard, n² is usually typedn^2, soO(n^2).
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
- No loop, the value is taken directly: O(1). Reading an array item by index, like
arr[i], is always O(1). - One loop over all the data: O(n).
- A loop inside a loop, both over all the data: O(n²), typed
O(n^2). - The data is halved at every step: O(log n).
- Two loops one after the other, not one inside the other: n + n = 2n steps, still O(n). The fixed multiplier, 2, is dropped.
⚠️ Common ways to misread Big O
- Testing only with small data. At n = 100, O(n²) is only 10,000 steps, and that feels instant. At n = 1,000,000 it becomes a trillion steps.
- Hidden loops.
arr.includes(x)inside a loop is really a loop inside a loop, becauseincludesitself checks the array's items one by one. The result is O(n²). - Big O isn't seconds. Big O describes how the steps grow. On small data, simple O(n²) code can be faster than complicated O(n) code. Big O tells you which code is faster once the data is big.
- Forgetting memory. Big O for the number of steps is called time complexity. Big O is also used for memory use (space complexity). Code that's fast but copies big data again and again can need more memory than the computer has.
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²).