Learn how Big O describes time complexity. Compare O(1), O(log n), O(n), and other growth rates with examples in JavaScript and Python.
Big O notation gives an upper bound on how fast an algorithm’s cost can grow as the input gets larger. For example, scanning an array of n items takes O(n) time because the work grows at most linearly. For the formal definition, see MIT’s notes on asymptotic complexity.
Let n be the number of items in a list. Reading its first item takes one indexed read. Finding its minimum by scanning a nonempty list takes n − 1 comparisons. If the list doubles in size, the first operation still takes one read and the scan needs about twice as many comparisons.
A linear scan also fits a looser quadratic upper bound, but O(n) describes its growth more precisely. Name the case you are analyzing. A search may finish on its first comparison in the best case and inspect every item in the worst case.
Compare the values of seven representative functions at input sizes 2, 4, and 8. For eight items, n² = 64, while 2ⁿ = 256. Equal distances on the plot multiply the value by the same factor. Actual operation counts depend on the algorithm and can include constant factors.
For the algorithm examples below, assume fixed-size items and constant-cost operations on each item, such as one indexed read.
| Growth | Meaning | Example pattern |
|---|---|---|
| Constant O(1) | Runtime does not grow with input size. | One operation |
| Logarithmic O(log n) | Runtime grows slowly as the input is halved repeatedly. | Binary search |
| Linear O(n) | Runtime grows with the number of input items. | One loop |
| Linearithmic O(n log n) | Runtime grows in proportion to n × log n. | Merge sort |
| Quadratic O(n²) | Runtime grows with the square of the input size. | Nested loops |
| Exponential O(2ⁿ) | Two choices per item double the number of possible outcomes. | Include or skip each item |
| Factorial O(n!) | Runtime grows factorially with each additional item. | Visiting every possible ordering |
O(1)An operation with constant growth has a fixed upper bound on its work regardless of input size. For a nonempty array, reading its first item uses one indexed access.
function getFirstItem(items) {
return items[0]; // Always performs a single operation
}Whether the input has a few items or many items, this pattern still performs one operation.
O(log n)Logarithmic algorithms cut the problem size down (usually in half) with every step.
function binarySearch(sortedItems, target) {
let left = 0;
let right = sortedItems.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (sortedItems[mid] === target) return mid;
if (sortedItems[mid] < target) {
left = mid + 1; // Halves the search space
} else {
right = mid - 1; // Halves the search space
}
}
return -1;
}Each unsuccessful comparison discards the midpoint and about half the candidates. Doubling the list adds at most one midpoint inspection to the worst case.
O(n)A single loop that processes each input item has linear growth.
function printAllItems(items) {
for (const item of items) {
console.log(item); // Runs once for each item: n times
}
}If the input contains n items, the loop performs work n times.
O(n log n)Linearithmic algorithms split the problem into smaller subproblems logarithmically (log n levels) while performing linear work (n) across each level.
For example, merging sorted runs of length 1 into runs of length 2 touches each item once. Merging those runs into runs of length 4 touches them all again. Eight items take three such merge levels: 1 → 2 → 4 → 8. The logarithms lesson explains how to count the levels.
O(n²)Nested loops can create quadratic growth when each loop goes through the input.
function printAllPairs(items) {
for (const first of items) {
for (const second of items) {
console.log(first, second); // Runs n * n times
}
}
}If the input contains n items, the inner operation is performed n × n times. As the input grows, quadratic runtime grows much faster than linear runtime.
O(2ⁿ)Imagine choosing toppings from a list. Each topping can be included or skipped, independently. One topping gives two choices, two toppings give four subsets, and three give eight. With n toppings there are 2ⁿ distinct subsets. Visiting each subset and doing one fixed-cost check per visit takes exponential time. Writing out every topping in every subset also costs time.
O(n!)Suppose three people, A, B, and C, stand in a line. There are three choices for the first place, two for the second, and one for the last: 3 × 2 × 1 = 6 possible lines. For n distinct people there are n! permutations. Testing every permutation takes at least that many visits. If you also copy out each full length-n ordering, writing the results alone takes O(n × n!) time.
Comparing growth rates
These representative functions grow increasingly quickly for large inputs:
1 → log n → n → n log n → n² → 2ⁿ → n!
Big O focuses on how running time grows as input size increases. To simplify an expression:
For example:
O(3n² + 5n + 7) → O(n²)The constant 3 is removed, and the lower-order terms 5n and 7 are ignored. The resulting bound is O(n²).
Decide what each loop counts before combining its costs. A scan of n items followed by a scan of m other items visits n + m items. Comparing every item of the first list with every item of the second visits n × m pairs. Keep both variables unless the problem tells you how the two lengths are related.
n) increases.O(3n) → O(n)).O(n² + 5n) → O(n²)).