Loading lesson…
New idea
Big-O describes how an algorithm's time (or memory) grows as input size n grows, ignoring constant factors -- it answers "what happens as n gets huge?". From fastest to slowest growth: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n) linearithmic, O(n^2) quadratic, O(2^n) exponential, O(n!) factorial. Every interview answer about an algorithm's efficiency is phrased in these terms.
function contains(items, target) { // O(n): worst case checks every item
for (const item of items) {
if (item === target) return true;
}
return false;
}
function first(items) { // O(1): same cost no matter the size
return items[0];
}Read the official docs ↗