Blog Hoch
Jun 2026·2 tup leghDe'wI' chenmoHta'

Big O Notation, real code rap

#algorithms#computer-science#javascript

Big O notation abstract examples rap taught (integer arrays sort, balanced trees search) 'ej people real features ship vaj quietly drop. mistake: same reasoning "simple" nested loop production data Daq server take down (data thousand rows past). Big O real code Daq ground.

Big O nuq measure

Big O algorithm running time (pagh memory use) input size grow vaj chay' grow describe - exact time pagh (CPU depend), growth curve shape. "O(n)" "input size roughly proportional". "O(n²)" "input size squared roughly proportional". constant factors exact instruction counts deliberately throw away; n large vaj nuq qaS qatlh.

constant factors scale Daq qatlh pagh, growth rate neH. n = 100 Daq O(n²) algorithm O(n) law' nom 'ach n large enough vaj reH lose - "if" pagh, crossover point "where".

O(1): constant time

Map pagh object Daq key value lookup map grow vaj slow pagh - key hash 'ej bucket jump 10 entries pagh 10 million same steps.

function getUserById(usersById, id) {
  return usersById.get(id); // O(1), regardless of map size
}

O(n): linear time

work input size proportional grow. array single pass (value search, sum, element Hoch transform) classic:

function findFirstAdmin(users) {
  for (const user of users) {
    if (user.role === 'admin') return user; // worst case: checks every user once
  }
  return null;
}

array double, worst-case work double. linear, everyday operations law' vaD fine.

O(n²): quadratic time - production silently kill

real bugs live. same data loop Daq loop nest work multiply:

// find duplicate emails - O(n²): for every user, scan every other user
function findDuplicateEmails(users) {
  const duplicates = [];
  for (let i = 0; i < users.length; i++) {
    for (let j = i + 1; j < users.length; j++) {
      if (users[i].email === users[j].email) {
        duplicates.push(users[i].email);
      }
    }
  }
  return duplicates;
}

100 users ~5,000 comparisons - instant. 100,000 users ~5 billion - demo test data Daq Dun, real production volume vaj timeout. pattern (inner loop pagh inner .find()/.includes() data outer loop rap grow) application code Daq accidental-quadratic bug most common.

fix almost always memory time trade: lookup structure wa'logh build (O(n)), single loop Daq O(1) lookups (total O(n)) - loop Daq O(n) lookups (total O(n²)) pagh.

// same result, O(n): one pass to build a Set, one pass to check membership
function findDuplicateEmails(users) {
  const seen = new Set();
  const duplicates = new Set();
  for (const user of users) {
    if (seen.has(user.email)) {
      duplicates.add(user.email);
    }
    seen.add(user.email);
  }
  return [...duplicates];
}

same output, input grow vaj fast - Map/Set trade: memory few pay, O(n) lookup O(1) lookup choH.

O(log n): logarithmic time

work input size law' slowly grow - step Hoch large fraction eliminate. sorted array binary search canonical: middle legh, half discard, repeat.

function binarySearch(sortedArr, target) {
  let low = 0;
  let high = sortedArr.length - 1;

  while (low <= high) {
    const mid = (low + high) >> 1;
    if (sortedArr[mid] === target) return mid;
    if (sortedArr[mid] < target) low = mid + 1;
    else high = mid - 1;
  }
  return -1;
}

sorted array 1,000,000 items search ~20 comparisons maximum (1,000,000 pagh) - O(log n) vs O(n) difference. database indexes (B-trees) large tables lookups fast: O(n) table scan O(log n) tree traversal choH.

O(n log n): sort-family complexity

efficient comparison-based sorting algorithms (merge sort, quicksort average case, Array.prototype.sort modern JS engines) O(n log n) - O(n²) law' Dun, comparison-based sorting theoretical floor. hand-rolled O(n²) sort (naive bubble sort) ghItlh vaj built-in sort lo' - nom 'ej correct.

own code complexity laD

rough rules everyday cases cover:

  • n items single loop: O(n).
  • loop another loop Daq nested (roughly same n): O(n²).
  • iteration Hoch search space halve (binary search, balanced tree descent): O(log n).
  • Array methods .includes(), .indexOf(), .find() O(n) Hoch - loop Daq wa' call = nested-loop pattern above (spelled differently). quadratic behavior plain sight Daq hide most common.
// looks like one loop, is actually O(n²): .includes() scans the array each call
function withoutDuplicates(items) {
  const result = [];
  for (const item of items) {
    if (!result.includes(item)) result.push(item); // O(n) call, inside an O(n) loop
  }
  return result;
}

// O(n): Set.has() is O(1), so the loop stays linear
function withoutDuplicatesFast(items) {
  return [...new Set(items)];
}

yaj

Big O milliseconds shave pagh (code already fine). patterns silently scale stop Sov. eyes train: O(n)-or-worse operation (.includes(), .find(), another loop) same growing collection loop Daq. wa' pattern "dev Daq Dun, production Daq fall over" bugs most.

chu'Semantic Versioning Qu' lIjqanFeature Toggling environment-based config rap