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. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.
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

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);

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);

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"));

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

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.

Membership optimization quiz
Quick check: Reduce nested scans.

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

Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Big-O basics, hot vs cold paths (beginner wins)” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Big-O basics, hot vs cold paths (beginner wins)”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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
- Big-O basics, hot vs cold paths (beginner wins)
- Avoiding leaks — closures, timers, references
- Profiling intro (Node/DevTools) — tiny timing habits