ON2 Quadratic Time

1 min read
Foundational1 min read
Rapid overview

ON2 Quadratic Time

TL;DR

O(n²) quadratic time means the work grows with the square of the input — doubling the data quadruples the cost. It's the signature of nested loops over the same collection or Array.includes inside a loop, and it's the complexity class that silently destroys throughput as data scales; most O(n²) hot paths can be rewritten to O(n) by indexing one side with a Set or Map.

How it works

for (let i = 0; i < items.length; i += 1) {
  for (let j = i + 1; j < items.length; j += 1) {
    compare(items[i], items[j]);
  }
}

Notes

  • Acceptable for small n.
  • Use hashing or indexing to reduce to O(n).

See also