Time Complexity
Time complexity describes how the work an algorithm does grows as its input grows. It is a shape, not a console.time reading on one machine.
Detailed Explanation
A function that is fine on twenty items can freeze a tab on twenty thousand. Time complexity is how you see that before you ship. You count operations in terms of n, the size of the input, and you keep the term that dominates when n is large.
Big-O notation writes that down. 3n + 7 is O(n). Constants and smaller terms drop out of the notation; they do not drop out of your latency graphs. An O(n) loop that does a database call per item is slower than an O(n log n) sort in memory.
Common classes in JavaScript:
| Notation | Name | Typical source |
|---|---|---|
O(1) | constant | arr[i], map.get, set.has |
O(log n) | logarithmic | binary search on a sorted array |
O(n) | linear | one pass, arr.includes, JSON.parse of n tokens |
O(n log n) | linearithmic | array.sort |
O(n^2) | quadratic | nested loops over the same list |
O(2^n) | exponential | naive subset recursion |
Reading complexity out of JS means looking at your loops and at the library calls inside them. arr.includes is O(n). Calling it inside another loop over arr is O(n^2) even though you wrote one for. set.has is O(1) average. Swapping an includes-in-a-loop for a Set is the usual interview fix.
Engine details sit under the notation. A packed array is O(1) index. A holey array can devolve to dictionary mode. push is amortised O(1). shift is O(n). sort is O(n log n) and has been required to be stable since ES2019. Hash tables (Map, Set, object keys) are O(1) average and O(n) in pathological collision cases you will not see with ordinary keys.
What it costs you: false precision. Big-O hides constant factors, GC pauses, and cache behaviour. Walking a linked list and walking a packed array are both O(n); the array wins by a wide margin. Measure after you know the shape is right.
Reach for complexity when choosing Map vs array scan, when a report times out only in production, and in interviews. Reach for a profiler when the shape is already linear and still slow.
Code Examples
function hasDuplicateQuadratic(items) {
for (let i = 0; i < items.length; i += 1) {
for (let j = i + 1; j < items.length; j += 1) {
if (items[i] === items[j]) return true;
}
}
return false;
}
function hasDuplicateLinear(items) {
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return true;
seen.add(item);
}
return false;
}
const emails = ["a@x.com", "b@x.com", "a@x.com"];
console.log(hasDuplicateQuadratic(emails)); // => true
console.log(hasDuplicateLinear(emails)); // => trueHidden quadratic: includes is already O(n).
// Hidden quadratic: includes is already O(n).
function missingIds(all, have) {
return all.filter((id) => !have.includes(id));
}
function missingIdsFast(all, have) {
const set = new Set(have);
return all.filter((id) => !set.has(id));
}
console.log(missingIdsFast([1, 2, 3], [2])); // => [ 1, 3 ]Common Gotchas
Library methods have their own cost
`includes`, `indexOf`, `splice`, and `shift` are not free. Nesting them is a loop you did not write.
`sort` without a comparator is still O(n log n) but wrong for numbers
complexity is not correctness. Pass `(a, b) => a - b`.
Average vs worst case
`Map.get` is O(1) typical. A hand-rolled object used as a map with hostile keys is a different story.
Amortised `push`
most pushes are O(1); a resize copies the array. The average over many pushes stays constant.