01

Foundation

Memory, Big O, the engine

GoalUnderstand how the computer actually runs your code. Stack vs Heap, value vs reference, Big O complexity, and what V8 actually is.

Lessons

Stack vs Heap

Your program has two memory regions.

The Stack is fast, ordered, and small. It stores primitives (number, string, boolean, null, undefined, symbol, bigint). Their size is fixed, so the engine reserves the slot instantly.

The Heap is large and unordered. It stores objects, arrays, functions — anything whose size can grow. The Stack only keeps a small address (reference) pointing into the Heap.

js
// Primitives: copied by VALUE
let x = 10;
let y = x;
y = 20;
console.log(x); // 10 — x is untouched

// Objects: copied by REFERENCE (address)
const a = { n: 0 };
const b = a;      // b gets a copy of the *address*, not the object
b.n = 5;
console.log(a.n); // 5 — same object in the Heap

This distinction is why React requires immutability: React compares addresses, not object contents.

Pass by Sharing — the precise model

JavaScript is always "pass by value" — but for objects, the value being copied is the address.

js
function update(obj, val) {
  obj.x = val;      // reaches the shared Heap object ✅
  obj = { x: 999 }; // only repoints the LOCAL parameter ❌ (caller unaffected)
}
const data = { x: 1 };
update(data, 5);
console.log(data.x); // 5  (not 999)
Mutating a property reaches the shared object. Reassigning the variable only changes the local copy of the address.

This is why user.age = 31; setUser(user) doesn't trigger a React re-render — same address.

Big O — measuring growth

Big O measures how work grows as input grows, not how fast it is on your machine today.

  • O(1) — constant. arr[0], map.get(key), set.has(x)
  • O(n) — linear. One loop over n items.
  • O(n²) — quadratic. A loop inside a loop. Where apps get slow.
  • O(log n) — halve the problem each step. Binary search.
js
// ❌ O(n²) — .includes() is O(n), called inside .filter() (O(n))
const common = arr1.filter(x => arr2.includes(x));

// ✅ O(n + m) — build a Set once (O(m)), then each .has() is O(1)
const set2 = new Set(arr2);
const common = arr1.filter(x => set2.has(x));

The instinct: whenever you see .find(), .includes(), or .indexOf() inside a loop, that's O(n²). Reach for a Map or Set.

V8: JIT compilation

V8 is the JS engine inside Chrome and Node.js. It uses JIT (Just-In-Time) compilation:

1. Ignition (interpreter) — runs code immediately, no warm-up delay. 2. TurboFan (compiler) — watches for "hot" functions called repeatedly, compiles them to fast machine code.

your code → Parser → AST → Ignition (runs fast)
                              ↓ (hot function detected)
                           TurboFan → machine code (runs very fast)

Engineering consequence: TurboFan assumes a function always receives the same types. Mixing number and string arguments causes deoptimization — the compiled version is thrown away. This is one hidden reason TypeScript improves performance.

Challenges4

Write working code. Run to see output. Check to grade.

CH1
Predict the mutation
Warm-up

Without changing the structure, predict what data.x will be after this runs. Write code that logs the correct value of data.x.

js
function update(obj, val) {
  obj.x = val;
  obj = { x: 999 };
}
const data = { x: 1 };
update(data, 5);
JavaScript
CH2
O(n) dedup with Set
Medium

Write findDuplicates(arr) that returns an array of values appearing more than once in O(n) time. Use a Set — do NOT use nested loops or .indexOf() inside a loop.

Expected: findDuplicates([1, 2, 2, 3, 3, 3, 4]) → [2, 3]

JavaScript
CH3
O(n) join with Map
Hard

You have users and orders. For each user, attach their orders. The naive approach is O(n×m). Write an O(n+m) version using a Map.

js
const users = [{id:1,name:"Ali"},{id:2,name:"Sara"}];
const orders = [{userId:1,item:"Book"},{userId:2,item:"Pen"},{userId:1,item:"Laptop"}];

Expected output (order of orders within each user doesn't matter): [{id:1,name:"Ali",orders:[...]}, {id:2,name:"Sara",orders:[...]}]

Log the result with console.log(JSON.stringify(result)).

JavaScript
CH4
const ≠ immutable
Warm-up

Prove that const does NOT make an object immutable. Write code that: 1. Declares const obj = { x: 1 } 2. Mutates obj.x to 42 3. Logs obj.x

Then in a comment, explain WHY const doesn't prevent this.

JavaScript

References