0Pricing
JavaScript Academy · Lesson

Big-O basics, hot vs cold paths (beginner wins)

Use Big-O intuition and focus on hot paths. Replace nested scans with Set/Map, avoid repeated work, and keep code small and clear.

Big-O basics, hot vs cold paths (beginner wins) is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Big picture

Goal: Quick wins without complex theory.

  • Big-O intuition: avoid double loops
  • Hot vs cold paths: optimize what runs most
  • Set/Map for fast lookups
  • Do work once, reuse results
Big-O basics, hot vs cold paths (beginner wins) — illustration 1

Nested scan = costly

Nesting scans multiplies work. Each includes walks the array again.

// Bad pattern: nested scan for membership (can be ~O(n^2))
const haystack = ["a","b","c","d","e"];
const needles = ["b","e","x"];
const foundSlow = [];

for (const n of needles) {
  // includes scans the array each time
  if (haystack.includes(n)) {
    foundSlow.push(n);
  }
}
console.log("slow found:", foundSlow);
Big-O basics, hot vs cold paths (beginner wins) — illustration 2

Set for membership

Create a Set once; use has for fast membership. This removes the inner scan.

// Better: build a Set once, then O(1)-ish checks (~O(n) overall)
const fastSet = new Set(haystack);
const foundFast = [];
for (const n of needles) {
  if (fastSet.has(n)) {
    foundFast.push(n);
  }
}
console.log("fast found:", foundFast);
Big-O basics, hot vs cold paths (beginner wins) — illustration 3

Map for counts

Map stores counts for O(1)-ish reads later; avoid rescanning arrays to recount.

// Build a frequency map once instead of scanning many times
const words = ["a","b","a","a","c","b"];
const freq = new Map();

for (const w of words) {
  const old = freq.get(w) || 0;
  freq.set(w, old + 1);
}

console.log("count a:", freq.get("a"));
console.log("count b:", freq.get("b"));
Big-O basics, hot vs cold paths (beginner wins) — illustration 4

Do it once (cache)

If a value repeats, cache the result. Keep caches small and clear for beginners.

// Do work once and reuse (tiny cache)
function heavy(x) {
  // pretend heavy math
  return x * x + 1;
}

const cache = new Map();

function heavyCached(x) {
  if (cache.has(x)) return cache.get(x);
  const val = heavy(x);
  cache.set(x, val);
  return val;
}

console.log("first:", heavyCached(5));
console.log("second:", heavyCached(5)); // cached
Big-O basics, hot vs cold paths (beginner wins) — illustration 5

Focus on hot paths

Hot path: runs many times (optimize here). Cold path: rare code (keep it simple).

  • Remove inner loops in hot code.
  • Use Set/Map on hot membership/count tasks.
  • Measure later; start with clarity.
Big-O basics, hot vs cold paths (beginner wins) — illustration 6

Membership optimization quiz

Quick check: Reduce nested scans.

Big-O basics, hot vs cold paths (beginner wins) — illustration 7

Recap

Recap: Avoid nested scans, use Set/Map for lookups and counts, and do work once. Focus on hot paths; keep cold code simple.

Big-O basics, hot vs cold paths (beginner wins) — illustration 8

Frequently asked questions

Is the “Big-O basics, hot vs cold paths (beginner wins)” lesson free?

Yes — the full text of “Big-O basics, hot vs cold paths (beginner wins)” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Big-O basics, hot vs cold paths (beginner wins)”?

Use Big-O intuition and focus on hot paths. Replace nested scans with Set/Map, avoid repeated work, and keep code small and clear. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Big-O basics, hot vs cold paths (beginner wins)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Big-O basics, hot vs cold paths (beginner wins)
  2. Avoiding leaks — closures, timers, references
  3. Profiling intro (Node/DevTools) — tiny timing habits
← Back to JavaScript Academy