What O(1), O(log n), O(n), O(n²) and O(2ⁿ) mean, how to find the Big O of your own code, and how to turn slow code fast, with step counts measured by running it.

By Michael Stromberg, Hack Reactor graduate (2016) · Last verified November 30, 2026
Big O notation is how programmers describe how fast code is, without a stopwatch. Instead of “this function takes 2 milliseconds,” which depends on your computer, Big O says how the work grows as the input grows: does doubling the data double the work, quadruple it, or barely change it?
This tutorial explains Big O with short JavaScript functions you can run yourself. Every step count in it was measured by running the code, so you can see the difference between O(n) and O(n²) in real numbers, not just in theory.
The short answer: Big O describes how the number of steps an algorithm takes grows with the size of its input, called n, in the worst case. O(1) means the work stays the same no matter how big the input gets; O(n) means it grows in step with the input; O(n²) means doubling the input makes four times the work. Lower is faster as the data grows.
![Big O growth chart and measured steps for 1,000 items, worst case: O(1), reading arr[0], 1 step; O(log n), binary search, 10 steps; O(n), finding the largest number, 1,000 steps; O(n log n), a good sort, about 10,000 steps (calculated); O(n squared), nested-loop duplicate check, 499,500 steps; O(2 to the n), a 302-digit number of steps](https://codingbootcampguides.com/wp-content/uploads/2026/12/big-o-notation-growth-chart.png)
What Big O measures
Big O counts steps, not seconds. A step is any basic operation: a comparison, an assignment, reading an array item. Seconds depend on the computer, the browser, and what else is running; the number of steps depends only on the code and the input.
Three ideas make Big O work:
- It’s about growth. Big O asks what happens as n gets large. For a list of 10 items, almost any code is fast. For 10 million, the difference between O(n) and O(n²) is the difference between a blink and hours.
- It describes the worst case. Searching a list might find the item first try, but Big O usually describes the longest it could take, because that’s the case you have to plan for.
- It ignores details. O(2n) and O(n + 5) are both just O(n). Constants and small terms stop mattering when n is huge, so Big O keeps only the part that grows fastest.
The common Big O complexities, fastest to slowest
O(1): constant time
The work doesn’t change with the input size. Reading an array item by its index takes one step whether the array has 10 items or 10 million:
function getFirst(items) {
return items[0];
}Looking up a key in an object, a Map, or a Set is also O(1) on average, which is why those structures are the go-to fix for slow code, as you’ll see below.
O(log n): logarithmic time
Each step cuts the remaining work in half. The classic example is binary search, which finds a value in a sorted array by checking the middle, then throwing away the half that can’t contain it:
function binarySearch(sorted, target) {
let low = 0;
let high = sorted.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (sorted[mid] === target) return mid;
if (sorted[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}Halving is powerful. In our tests, binary search needed at most 10 steps for 1,000 items and only 20 for 1,000,000. Doubling the array adds just one more step. (Here “log” means log base 2: how many times you can halve n before reaching 1.) Balanced binary search trees get their speed the same way.
O(n): linear time
The work grows in step with the input. To find the largest number in an unsorted array, you have to look at every number once:
function findMax(numbers) {
let max = -Infinity;
for (const n of numbers) {
if (n > max) max = n;
}
return max;
}Twice the numbers, twice the steps. Most single loops over an array are O(n), and so are many built-in methods that loop for you, like includes, indexOf, map, and filter.
O(n log n): linearithmic time
This is the speed of the best general-purpose sorting algorithms, such as merge sort. JavaScript’s built-in array.sort() is O(n log n) in modern engines (V8, the engine in Chrome and Node.js, uses an algorithm called TimSort). It’s a little slower than O(n) but far faster than O(n²), so “sort first, then do something clever” is a common way to speed up code.
O(n²): quadratic time
Usually a loop inside a loop over the same data. This function checks whether an array has any duplicates by comparing every item with every item after it:
function hasDuplicateSlow(items) {
for (let i = 0; i < items.length; i++) {
for (let j = i + 1; j < items.length; j++) {
if (items[i] === items[j]) return true;
}
}
return false;
}For 1,000 items that’s 499,500 comparisons. For 1,000,000 items it’s about 500 billion. Nested loops are the most common reason beginner code is slow, and they’re easy to spot once you look for them.
O(2ⁿ): exponential time
The work doubles with every extra item. A recursive function that calls itself twice per call, like this naive Fibonacci function, quickly gets out of hand:
function fib(n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}fib(30) makes 2,692,537 function calls, and fib(40) makes 331,160,281. Strictly speaking, its growth is a little slower than 2ⁿ, but it’s exponential either way, and anything exponential is unusable beyond small inputs.
How the step counts compare
Here are the steps each function takes in its worst case, measured by running the code above (the n² row for 1,000,000 items is calculated from the formula, because running it would take hours):
| n (input size) | O(log n) binary search | O(n) find max | O(n²) nested loops |
|---|---|---|---|
| 10 | 4 | 10 | 45 |
| 100 | 7 | 100 | 4,950 |
| 1,000 | 10 | 1,000 | 499,500 |
| 10,000 | 14 | 10,000 | 49,995,000 |
| 1,000,000 | 20 | 1,000,000 | 499,999,500,000 |
Look at how differently the columns grow. Going from 1,000 to 1,000,000 items, a thousand times more data, adds 10 steps to binary search, multiplies the O(n) loop’s work by 1,000, and multiplies the nested loops’ work by about a million.
How to find the Big O of your code
You don’t need math to work out Big O. Read the code and apply these rules:
- Count the loops over the input. One loop over n items is O(n). A loop inside a loop over the same items is O(n²).
- Loops one after another add; loops inside each other multiply. Two separate loops are O(n + n) = O(2n), which simplifies to O(n). A loop inside a loop is O(n × n) = O(n²).
- Drop constants and smaller terms. O(3n + 10) becomes O(n), and O(n² + n) becomes O(n²). Only the fastest-growing part matters for large inputs.
- Different inputs get different letters. Looping over array
ainside a loop over arraybis O(a × b), not O(n²), because the two arrays can have different sizes. - Watch for hidden loops.
includes,indexOf,slice, spreading with..., andshifteach loop over the array behind the scenes. Calling one inside a loop quietly makes your code O(n²). - Halving means log n. If each step cuts the remaining work in half, as in binary search, the loop is O(log n).
Fixing slow code: from O(n²) to O(n)
The most useful thing Big O teaches is how to make code faster by changing the approach, not by tweaking lines. Here’s the duplicate check again, rewritten with a Set, which remembers what we’ve already seen:
function hasDuplicate(items) {
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return true;
seen.add(item);
}
return false;
}Checking seen.has(item) is O(1) on average, so the whole function is one loop: O(n). It trades a little memory, the Set, for a lot of speed.

How much faster? On an array of 100,000 unique numbers, on the Apple Silicon Mac we tested with, the nested-loop version took about 3.9 seconds and the Set version about 6.5 milliseconds. Your times will differ, but the gap won’t: one version does about 5 billion comparisons, and the other does 100,000 lookups.
This same trick, replacing an inner search with a hash lookup, is the standard answer to the famous Two Sum interview problem, and our hash table tutorial shows how a structure like Set makes lookups O(1) in the first place.
Recursion and memoization
Recursive functions can hide exponential work, like the fib function above, which recalculates the same values over and over: fib(30) computes fib(2) hundreds of thousands of times. The fix is memoization: remember each answer the first time you calculate it.
function fibMemo(n, memo = new Map()) {
if (n < 2) return n;
if (memo.has(n)) return memo.get(n);
const result = fibMemo(n - 1, memo) + fibMemo(n - 2, memo);
memo.set(n, result);
return result;
}| n | Calls without memoization, O(2ⁿ) | Calls with memoization, O(n) |
|---|---|---|
| 10 | 177 | 19 |
| 20 | 21,891 | 39 |
| 30 | 2,692,537 | 59 |
| 40 | 331,160,281 | 79 |
Each value is now calculated once, so the function is O(n). Our memoization tutorial builds a reusable version that works with any function, and Recursion Explained in JavaScript covers recursion itself.
Space complexity
Big O also describes memory. Space complexity counts how much extra memory a function uses as the input grows, not counting the input itself.
findMaxuses one variable no matter how big the array is: O(1) space.- The
Setversion ofhasDuplicatecan store every item: O(n) space. - The memoized
fibMemostores one answer per number: O(n) space. Recursion also uses memory for each call that’s still waiting to finish.
Faster code often uses more memory, as both fixes above did. That’s usually a good trade, but it’s worth saying out loud in an interview.
Big O of common JavaScript operations
The JavaScript specification doesn’t promise exact costs for most operations, but modern engines (V8 in Chrome and Node.js, SpiderMonkey in Firefox, and JavaScriptCore in Safari) behave like this in practice. The spec does require Map and Set lookups to be faster than O(n) on average.
| Operation | Big O | Why |
|---|---|---|
arr[i], arr.length | O(1) | Arrays store items by position |
push(), pop() | O(1) | They work at the end, so nothing else moves |
shift(), unshift() | O(n) | Every other item has to move one position |
splice() | O(n) | Items after the change have to move |
includes(), indexOf(), find() | O(n) | They check items one by one |
map(), filter(), forEach(), reduce() | O(n) | They visit every item |
slice(), concat(), [...arr] | O(n) | They copy items into a new array |
sort() | O(n log n) | Efficient sorting algorithm |
set.has(), set.add(), map.get(), map.set() | O(1) on average | Hash tables jump straight to the right slot |
obj[key] | O(1) on average | Objects are also hash-based lookups |
If you need a queue that removes items from the front a lot, shift() on a big array gets slow. A linked list removes from the front in O(1).
Practice: what’s the Big O?
Work out each function’s time complexity before opening the answer.
1. Two loops, one after the other:
function minAndMax(numbers) {
let min = Infinity;
for (const n of numbers) min = Math.min(min, n);
let max = -Infinity;
for (const n of numbers) max = Math.max(max, n);
return [min, max];
}Show the answer
O(n). Each loop is O(n), and loops one after another add: O(n + n) = O(2n), which simplifies to O(n). It uses O(1) extra space.
2. Items that appear in both arrays:
function commonItems(a, b) {
const result = [];
for (const item of a) {
if (b.includes(item)) result.push(item);
}
return result;
}Show the answer
O(a × b), where a and b are the two arrays’ lengths. The loop runs once per item in a, and b.includes() is a hidden loop over b. Turning b into a Set first makes it O(a + b).
3. How many times can you halve a number?
function countHalvings(n) {
let count = 0;
while (n > 1) {
n = Math.floor(n / 2);
count++;
}
return count;
}Show the answer
O(log n). n is cut in half on every turn, so the loop runs about log₂(n) times: 10 times for 1,024, and 19 times for 1,000,000.
Big O in coding interviews
In a technical interview, you’re usually expected to state the time and space complexity of your solution, and often to improve it. A good pattern:
- Start with a working solution, even a slow one, and say its Big O: “This is O(n²) because of the nested loop.”
- Look for repeated work. Are you searching the same data again and again? A
SetorMapoften turns O(n²) into O(n). - Name the trade-off: “This is O(n) time but O(n) space, because of the Set.”
- Use n carefully. Say what n means (the array’s length, the string’s length, the number of nodes), and use separate letters for separate inputs.
Getting ready for a bootcamp rather than an interview? Our 10 JavaScript practice problems are a good place to practice spotting the slow parts of your own code.
From Michael, Hack Reactor ’16: Big O is one of those topics that looks academic until the day your code times out on real data. My advice: whenever you write a loop, ask yourself what’s inside it. If there’s another loop, or an includes, or a find, that’s O(n²), and there’s usually a Set or Map that fixes it. That one habit covers most of what interviewers look for.
Big O Notation FAQ
What is Big O notation in simple terms?
A way to describe how much more work code does as its input grows. O(1) stays the same, O(n) grows in step with the input, and O(n²) grows with the square of the input, so doubling the data means four times the work.
Is O(1) always faster than O(n)?
Not for small inputs. Big O ignores constants, so an O(1) operation that does a lot of fixed work can be slower than an O(n) loop over five items. Big O tells you which code wins as the data gets large.
What does the “O” stand for?
Order, as in “order of growth.” Big O comes from mathematics, where it describes an upper bound on how fast a function grows.
What is the Big O of JavaScript’s sort()?
O(n log n) in modern engines. V8, used by Chrome and Node.js, uses TimSort. Sorting is never faster than O(n log n) for general data compared item by item.
What is the difference between time and space complexity?
Time complexity counts the steps code takes as the input grows. Space complexity counts the extra memory it uses. Faster solutions often use more memory, such as a Set that remembers items already seen.
Do I need to know Big O for a coding bootcamp?
Not to get in, but many bootcamps teach it alongside data structures and algorithms, because it comes up in most technical interviews. Knowing the basics beforehand makes those weeks easier.
Michael has a diverse background in software engineering, mechanical engineering, economics, and management. After a successful career in mechanical engineering, Michael decided to become a software engineer and attended the Hack Reactor coding bootcamp. Michael enjoys writing about his experiences and helping others start their journeys in software development.
