These are the notes for JavaScript episode 31. Last episode, tests proved our code is correct. But correct code can still be painfully slow, or slowly eat all the memory on a server until it crashes. A restaurant app counts returning customers by checking every phone number against a growing array with includes: 1,000 orders take about a millisecond, 10,000 take well over a hundred milliseconds, and 40,000 take around a second. Ten times the data, a hundred times the time. You'll find out why (an idea called Big O), invent the fix (a Set, which jumps straight to the answer), learn to measure instead of guessing (console.time, performance.now), and see slow code freeze the event loop so that a 10 ms timer fires a whole second late. Then memory: the heap, the garbage collector, a menu cache that climbs about 80 MB every round until FATAL ERROR: ... JavaScript heap out of memory, and the fix, a cache with a limit. Every output below is real Node.js output (node main.js, Node 24). Timings and megabyte numbers change on every run and on every computer; yours will differ, but the shape (what grows, what stays flat) will be the same.
Builds on: loops (episode 6), arrays and includes (7), objects (8), closures (10), Map and Set (21), the event loop and timers (24), async code (27-28), pure functions (29), testing (30). Leads to: TypeScript basics (32).
The real-life ideas
The society's letter boxes (why Set is fast)
A postman delivers letters in a big housing society.
| In the society | In JavaScript |
|---|---|
| One long list of residents at the gate, read from the top for every letter | array.includes(x): checks items one by one, O(n) |
| A letter box for every flat: a letter for B-402 goes straight to box B-402 | set.has(x) / map.get(key): jumps straight to the right place, O(1) |
| 500 flats or 5,000 flats: still one walk to one box | the lookup takes the same time, whatever the size |
One cook, one long order (why slow code freezes a server)
| In the restaurant | In Node.js |
|---|---|
| The restaurant has one cook | Node runs your JavaScript on one thread |
| A 200-plate order: he can't stop in the middle | a long synchronous loop runs to the end before anything else |
| The doorbell rings, nobody answers for ages | timers, new requests and callbacks all wait in the queue |
| Cook faster, and the bell is answered on time | faster code keeps the event loop free |
| Hire a second cook for the huge orders | worker_threads: heavy work on another thread |
Tables in a busy restaurant (memory and leaks)
| In the restaurant | In JavaScript |
|---|---|
| The dining hall, with only so many tables | the heap, where objects, arrays and strings live |
| A group of guests takes a table | creating an object uses some heap memory |
| Waiters clear tables when the guests have left | the garbage collector frees objects nothing points to any more |
| A "Reserved" sign that is never taken down | a forgotten reference: the object can never be freed |
| More and more reserved tables, until no one can sit | a memory leak, then "heap out of memory" |
The problem: code that gets slow
makeOrders(n) just makes test data: a list of n phone numbers where every number appears twice. (Don't worry about its maths: i * 7 % (n / 2) jumps around the numbers 0 to n/2 - 1, so each one appears twice.)
countReturning keeps an array called seen. For each phone: if seen already includes it, that's a returning customer; otherwise push it into seen.
function makeOrders(n) {
const phones = [];
for (let i = 0; i < n; i++) phones.push("98" + (i * 7 % (n / 2)));
return phones;
}
function countReturning(phones) {
const seen = [];
let returning = 0;
for (const phone of phones) {
if (seen.includes(phone)) returning++;
else seen.push(phone);
}
return returning;
}
for (const n of [1000, 10000, 40000]) {
const phones = makeOrders(n);
console.time(`${n} orders`);
const returning = countReturning(phones);
console.timeEnd(`${n} orders`);
}
Real output (one run):
1000 orders: 0.848ms
10000 orders: 184.551ms
40000 orders: 1.017s
The code is correct. But look at the jump: 10 times more orders (1,000 → 10,000) did not take 10 times longer, it took over 100 times longer. And 40,000 orders, which a busy restaurant chain gets before lunch, takes about a second for one simple count.
console.time(label) starts a stopwatch with a name; console.timeEnd(label) with the same name stops it and prints label: time. Bigger times are printed in seconds (1.017s).
Big O: how the work grows
Why is it slow? includes doesn't magically know the answer. It starts at the first item of seen and compares one by one, until it finds the phone or reaches the end. And seen keeps getting longer.
- 1,000 orders: up to ~1,000 reads for each order.
- 10,000 orders: up to ~10,000 reads for each order.
- 40,000 orders: up to ~40,000 reads for each order.
So the total work is about n orders × n reads = n². Programmers write this as O(n²) ("O of n squared", or quadratic).
Big O describes how the amount of work grows as the data grows. It ignores the exact milliseconds (those depend on the computer) and keeps only the shape.
| Big O | Name | Example | 1,000 items | 10,000 items | 1,000,000 items |
|---|---|---|---|---|---|
| O(1) | constant | set.has(x), map.get(k), arr[5] |
1 step | 1 step | 1 step |
| O(log n) | logarithmic | binary search in a sorted array (episode 37) | ~10 | ~14 | ~20 |
| O(n) | linear | one loop over the data, arr.includes(x) once |
1,000 | 10,000 | 1,000,000 |
| O(n log n) | "n log n" | a good sort, arr.sort() |
~10,000 | ~140,000 | ~20,000,000 |
| O(n²) | quadratic | includes inside a loop, a loop inside a loop |
1,000,000 | 100,000,000 | 1,000,000,000,000 |
Rules of thumb:
- 10× the data with O(n) = 10× the work. With O(n²) = 100× the work.
- A loop that calls
includes,indexOf,find,filterorsomeon another big array inside it is usually O(n²) in disguise: those methods are loops too. - Big O is about large inputs. For 20 items, nothing matters. For 100,000 items, O(n²) is the difference between "instant" and "the page timed out".
The fix: Set goes straight to the answer
A Set (episode 21) stores each value in a place it can work out from the value itself (a hash table: the value is turned into a number that says where it lives, like the flat number on a letter box). So has jumps straight there: O(1) on average, whatever the size.
Change three words: [] → new Set(), includes → has, push → add. And add a million orders:
function makeOrders(n) {
const phones = [];
for (let i = 0; i < n; i++) phones.push("98" + (i * 7 % (n / 2)));
return phones;
}
function countReturning(phones) {
const seen = new Set();
let returning = 0;
for (const phone of phones) {
if (seen.has(phone)) returning++;
else seen.add(phone);
}
return returning;
}
for (const n of [1000, 10000, 40000, 1000000]) {
const phones = makeOrders(n);
console.time(`${n} orders`);
const returning = countReturning(phones);
console.timeEnd(`${n} orders`);
}
Real output:
1000 orders: 0.207ms
10000 orders: 2.718ms
40000 orders: 8.976ms
1000000 orders: 526.103ms
40,000 orders in under 10 ms instead of a second, and a million in about half a second. The slow version would need many minutes for a million (40,000 took ~1 s; 25 times more data is ~625 times more work: over 10 minutes). The loop is now O(n): one pass, and one O(1) lookup per order.
The most important performance rule: pick the data structure that answers your question directly. Asking "have I seen this before?" → Set. Asking "what is the value for this key?" or "how many times?" → Map.
Map for counting
The same idea counts how many orders each customer made, in one pass:
const orders = ["9812", "9845", "9812", "9877", "9812", "9845"];
function ordersPerCustomer(phones) {
const count = new Map();
for (const phone of phones) {
count.set(phone, (count.get(phone) ?? 0) + 1);
}
return count;
}
console.log(ordersPerCustomer(orders));
Map(3) { '9812' => 3, '9845' => 2, '9877' => 1 }
count.get(phone) ?? 0 gives 0 the first time (when get returns undefined), then + 1.
Measure, don't guess: console.time and performance.now
How did we know which part was slow? We measured. Programmers are famously bad at guessing where the time goes, so always measure before and after a change.
console.time("label")/console.timeEnd("label"): quick stopwatch, printslabel: 12.3ms.performance.now(): the time in milliseconds since the program started, with decimals (built into Node and browsers, norequireneeded). Save it before, subtract after, and print it however you like.
function makeOrders(n) {
const phones = [];
for (let i = 0; i < n; i++) phones.push("98" + (i * 7 % (n / 2)));
return phones;
}
function countReturning(phones) {
const seen = new Set();
let returning = 0;
for (const phone of phones) {
if (seen.has(phone)) returning++;
else seen.add(phone);
}
return returning;
}
const phones = makeOrders(200000);
const start = performance.now();
const returning = countReturning(phones);
const ms = performance.now() - start;
console.log(`${returning} returning customers in ${ms.toFixed(1)} ms`);
console.time("orders");
countReturning(phones);
console.timeEnd("order");
Real output:
100000 returning customers in 43.7 ms
(node:3311) Warning: No such label 'order' for console.timeEnd()
(Use `node --trace-warnings ...` to show where the warning was created)
performance.now() worked. But the second timing never printed: the stopwatch was called "orders" and timeEnd asked for "order". The labels must match exactly. It's only a warning (printed to stderr), so the program keeps running, which makes it easy to miss.
Tips for fair measurements:
- Time only the part you care about (not the test-data creation): that's why
makeOrdersruns beforeconsole.time. - Run it a few times. The first run is often slower, because V8 (Node's JavaScript engine) hasn't optimised the function yet.
- Compare on the same machine, with the same input size.
Date.now()also works, but only in whole milliseconds;performance.now()is more precise.
Slow code freezes the server: blocking the event loop
Remember the event loop (episode 24): Node runs your JavaScript on one thread. Timers, network requests and callbacks wait in a queue, and the event loop runs them only when the current code has finished. A long synchronous loop is the cook with a 200-plate order: nothing else happens until it's done.
function makeOrders(n) {
const phones = [];
for (let i = 0; i < n; i++) phones.push("98" + (i * 7 % (n / 2)));
return phones;
}
function countReturning(phones) {
const seen = [];
let returning = 0;
for (const phone of phones) {
if (seen.includes(phone)) returning++;
else seen.push(phone);
}
return returning;
}
const start = Date.now();
setTimeout(() => {
console.log(`new order alert, after ${Date.now() - start} ms`);
}, 10);
countReturning(makeOrders(40000));
console.log(`daily report done, after ${Date.now() - start} ms`);
Real output:
daily report done, after 881 ms
new order alert, after 885 ms
We asked for the alert after 10 ms. It came after almost 900 ms. The timer was ready at 10 ms, but the one thread was stuck inside our loop, so the event loop couldn't run it. On a real server, that's every customer's request frozen for almost a second.
Same code with the Set version of countReturning:
daily report done, after 17 ms
new order alert, after 24 ms
The report finishes in a few milliseconds, and the alert fires straight after. Faster code is also a more responsive server.
When the work really is heavy
Sometimes the work can't be made fast (resizing images, a huge calculation). Two options:
1. Split it into chunks with setImmediate, so the event loop gets a turn between chunks:
const start = Date.now();
setTimeout(() => console.log(`order alert, after ${Date.now() - start} ms`), 10);
let i = 0, sum = 0;
function work() {
const end = Math.min(i + 1e7, 1e8);
for (; i < end; i++) sum += i % 7;
if (i < 1e8) setImmediate(work);
else console.log(`report done: ${sum}, after ${Date.now() - start} ms`);
}
work();
order alert, after 107 ms
report done: 299999995, after 1118 ms
The alert now fires after the first chunk (about 100 ms) instead of waiting for the whole second. Smaller chunks = a more responsive loop.
2. A second cook: worker_threads. A worker runs JavaScript on another thread, so the main thread stays free:
const { Worker, isMainThread, parentPort } = require("node:worker_threads");
if (isMainThread) {
const start = Date.now();
setTimeout(() => console.log(`order alert, after ${Date.now() - start} ms`), 10);
const worker = new Worker(__filename);
worker.on("message", (sum) => console.log(`report from worker: ${sum}, after ${Date.now() - start} ms`));
} else {
let sum = 0;
for (let i = 0; i < 1e9; i++) sum += i % 7;
parentPort.postMessage(sum);
}
order alert, after 24 ms
report from worker: 2999999997, after 5874 ms
The worker spent almost 6 seconds counting, and the alert still fired on time. But workers are a bigger tool (separate memory, messages back and forth). Always fix the algorithm first.
Memory: the heap and the garbage collector
Every object, array, string and function you create needs memory. JavaScript keeps them in an area called the heap. You never free memory yourself (there's no free() like in C). Instead, the garbage collector (GC) does it:
- An object is reachable if your program can still get to it: from a variable, from inside another reachable object, from a running timer's function, from a closure, from a
Map, and so on. - Anything unreachable is garbage. The GC frees its memory.
- The GC runs when it decides (usually when a lot of memory has been used), not when your object becomes garbage. So memory goes up and down.
process.memoryUsage()
const toMB = (bytes) => (bytes / 1024 / 1024).toFixed(1) + " MB";
const m = process.memoryUsage();
console.log("rss: ", toMB(m.rss));
console.log("heapTotal:", toMB(m.heapTotal));
console.log("heapUsed: ", toMB(m.heapUsed));
const big = new Array(1_000_000).fill("chai");
console.log("after a 1,000,000-item array, heapUsed:", toMB(process.memoryUsage().heapUsed));
Real output:
rss: 41.9 MB
heapTotal: 5.6 MB
heapUsed: 3.9 MB
after a 1,000,000-item array, heapUsed: 11.9 MB
| Field | Meaning |
|---|---|
heapUsed |
heap memory your objects use right now (including garbage not collected yet) |
heapTotal |
heap memory V8 has reserved (it grows as needed) |
rss |
"resident set size": all the memory the whole Node process uses (heap + code + buffers + more) |
external |
memory for things outside the JavaScript heap, like Buffers |
The values are in bytes; divide by 1024 * 1024 for megabytes (MB). A million-item array of the same string costs about 8 MB (8 bytes per slot, the string itself is stored once).
A memory leak
A memory leak is memory that is never given back because something still points to it, even though the program will never use it again. In a short script you never notice. In a server that runs for weeks, it grows until the process dies.
A classic leak: a cache that never forgets. getMenu builds a menu for every request and saves it in a Map "to reuse it later":
const cache = new Map();
function getMenu(requestId) {
const menu = { id: requestId, items: new Array(200).fill("dosa") };
cache.set(requestId, menu);
return menu;
}
const mb = () => Math.round(process.memoryUsage().heapUsed / 1024 / 1024);
for (let round = 1; round <= 8; round++) {
for (let i = 0; i < 50000; i++) getMenu(round * 1000000 + i);
console.log(`after ${round * 50000} requests: ${mb()} MB, cache ${cache.size}`);
}
Real output:
after 50000 requests: 88 MB, cache 50000
after 100000 requests: 172 MB, cache 100000
after 150000 requests: 260 MB, cache 150000
after 200000 requests: 333 MB, cache 200000
after 250000 requests: 414 MB, cache 250000
after 300000 requests: 508 MB, cache 300000
after 350000 requests: 589 MB, cache 350000
after 400000 requests: 669 MB, cache 400000
It climbs by about 80 MB every round and never comes down. Every request id is different, so no menu is ever reused, yet every menu is still inside cache. The garbage collector sees a reference to each one and must keep all 400,000.
How a leak ends
A real server runs for weeks, not eight rounds. To fast-forward, run the same file with a tiny heap limit of 40 MB (--max-old-space-size sets the limit in MB):
$ node --max-old-space-size=40 main.js
<--- Last few GCs --->
[1582:0x37ebb000] 227 ms: Mark-Compact 54.7 (87.4) -> 54.2 (118.9) MB, pooled: 0 MB, 67.22 / 0.00 ms (average mu = 0.644, current mu = 0.413) allocation failure; scavenge might not succeed
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----
...
It dies before the first round finishes. Mark-Compact is the garbage collector's big clean-up: it worked hard and freed almost nothing (54.7 → 54.2 MB), because everything was still referenced. This is a fatal error, not an exception: try/catch cannot catch it. The process is gone, every customer gets an error page, and after a restart the leak starts again.
The fix: a cache with a limit
A cache must know when to forget. Keep at most LIMIT menus; after adding one, if the cache is too big, delete the oldest entry.
A Map remembers insertion order, so cache.keys() gives the keys oldest first, and cache.keys().next().value is the oldest key.
const cache = new Map();
const LIMIT = 1000;
function getMenu(requestId) {
const menu = { id: requestId, items: new Array(200).fill("dosa") };
cache.set(requestId, menu);
if (cache.size > LIMIT) cache.delete(cache.keys().next().value);
return menu;
}
const mb = () => Math.round(process.memoryUsage().heapUsed / 1024 / 1024);
for (let round = 1; round <= 8; round++) {
for (let i = 0; i < 50000; i++) getMenu(round * 1000000 + i);
console.log(`after ${round * 50000} requests: ${mb()} MB, cache ${cache.size}`);
}
Real output:
after 50000 requests: 85 MB, cache 1000
after 100000 requests: 169 MB, cache 1000
after 150000 requests: 54 MB, cache 1000
after 200000 requests: 133 MB, cache 1000
after 250000 requests: 78 MB, cache 1000
after 300000 requests: 25 MB, cache 1000
after 350000 requests: 106 MB, cache 1000
after 400000 requests: 54 MB, cache 1000
The cache stays at exactly 1,000. Memory goes up, then drops, up, then drops: old menus become garbage, but heapUsed counts them until the garbage collector decides to run; when it does, memory drops. This sawtooth is healthy. What's dangerous is a line that only goes up.
Shape of heapUsed over time |
Meaning |
|---|---|
| Up and down (sawtooth), around the same level | healthy: garbage is created and collected |
| Steadily up, never back down, for hours | a leak: something keeps references |
This "delete the oldest" cache is a simple FIFO cache (first in, first out). A smarter version, LRU (least recently used), moves an entry to the newest end every time it is read, so popular entries stay. See the mini project.
Other leaks to watch for
- Caches and global arrays that only grow. Give them a limit, or remove entries when they're done.
setIntervalnever cleared. A running timer keeps its function alive, and everything that function uses.- Event listeners added again and again (every time a screen opens) and never removed.
- Closures (episode 10) that hold on to big data they no longer need.
The timer leak
const todaysOrders = ["dosa", "idli", "vada"];
function openOrderScreen(orders) {
setInterval(() => {
console.log(orders.length, "orders on screen");
}, 1000);
}
openOrderScreen(todaysOrders);
openOrderScreen(todaysOrders);
setTimeout(() => process.exit(0), 2500); // only so this demo ends
3 orders on screen
3 orders on screen
3 orders on screen
3 orders on screen
Two calls, two timers, both printing every second, forever (without the process.exit line, this program never ends). Each timer's function is a closure holding orders, so orders can never be freed. Open the screen a hundred times: a hundred timers.
The fix: keep the timer id and return a function that clears it.
const todaysOrders = ["dosa", "idli", "vada"];
function openOrderScreen(orders) {
const timer = setInterval(() => {
console.log(orders.length, "orders on screen");
}, 1000);
return () => clearInterval(timer);
}
const close = openOrderScreen(todaysOrders);
setTimeout(() => {
close();
console.log("screen closed, timer stopped");
}, 2500);
3 orders on screen
3 orders on screen
screen closed, timer stopped
After close(), the timer is gone and the program ends by itself. (In the video's slide, todaysOrders is assumed to exist; here it's defined so the code runs.)
The listener leak
Node even warns you about this one:
const { EventEmitter } = require("node:events");
const kitchen = new EventEmitter();
function openScreen() {
kitchen.on("order", (dish) => console.log("new order:", dish));
}
for (let i = 0; i < 11; i++) openScreen();
console.log("listeners:", kitchen.listenerCount("order"));
listeners: 11
(node:3760) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 order listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
Fix: remove the listener when the screen closes, with off (the same function you passed to on):
const { EventEmitter } = require("node:events");
const kitchen = new EventEmitter();
function openScreen() {
const show = (dish) => console.log("new order:", dish);
kitchen.on("order", show);
return () => kitchen.off("order", show);
}
for (let i = 0; i < 11; i++) {
const close = openScreen();
close();
}
const close = openScreen();
kitchen.emit("order", "masala dosa");
console.log("listeners:", kitchen.listenerCount("order"));
new order: masala dosa
listeners: 1
Don't "fix" the warning with setMaxListeners(100): that only hides the leak.
The recipe for fast, lean code
- Measure (
console.time,performance.now,process.memoryUsage). - Find the slow part: usually a loop inside a loop, often hidden as
includes/find/indexOfinside a loop. - Fix the algorithm: usually a
Setor aMap. A better Big O beats any micro-trick. - Measure again, to prove it got better (and keep your tests from episode 30 green, to prove it's still correct).
Things that usually don't matter: for vs forEach, let vs const, short variable names. Fix the Big O first.
Common mistakes (and what really happens)
| You wrote | What happens | Why | Fix |
|---|---|---|---|
if (seen.includes(x)) ... inside a loop |
40,000 items: ~1 s instead of ~10 ms | O(n²): includes reads the list every time |
const seen = new Set() and seen.has(x) |
console.time("orders") ... console.timeEnd("order") |
Warning: No such label 'order' for console.timeEnd() and no time printed |
the labels differ | use exactly the same label |
| timing including the data creation | the number is too big and misleading | measured the wrong thing | start the timer right before the code you're testing |
| a long synchronous loop in a server | a 10 ms timer fires ~1 s late; every request waits | one thread, the event loop is blocked | faster algorithm; chunks with setImmediate; worker_threads |
cache.set(id, value) forever |
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory |
every entry is still referenced | limit the cache, delete the oldest key |
try { ... } catch around an out-of-memory |
the process still dies | it's a fatal error, not an exception | fix the leak |
setInterval(...) every time a screen opens |
timers pile up, memory climbs, the program never ends | never cleared | keep the id, clearInterval(timer) |
emitter.on(...) every time a screen opens |
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. |
listeners never removed | emitter.off(event, sameFunction) |
emitter.setMaxListeners(100) to silence that |
the warning goes away, the leak stays | hides the symptom | remove listeners |
panicking at a rising heapUsed once |
it drops later | the GC runs when it chooses | watch the trend: a sawtooth is fine, a line that only goes up is a leak |
Mini project: a fast, memory-safe daily report
Build a report of customers and returning customers per day, with an LRU cache of at most 3 days, so asking for the same day again is instant but memory stays bounded.
// Mini project: a fast, memory-safe "returning customers" report with a small LRU cache
function makeOrders(n) {
const phones = [];
for (let i = 0; i < n; i++) phones.push("98" + (i * 7 % (n / 2)));
return phones;
}
class LRUCache {
constructor(limit) {
this.limit = limit;
this.map = new Map();
this.hits = 0;
this.misses = 0;
}
get(key, compute) {
if (this.map.has(key)) {
const value = this.map.get(key);
this.map.delete(key); // move to the newest end
this.map.set(key, value);
this.hits++;
return value;
}
this.misses++;
const value = compute(key);
this.map.set(key, value);
if (this.map.size > this.limit) this.map.delete(this.map.keys().next().value);
return value;
}
}
function report(phones) {
const visits = new Map();
for (const phone of phones) visits.set(phone, (visits.get(phone) ?? 0) + 1);
let returning = 0;
for (const count of visits.values()) if (count > 1) returning++;
return { customers: visits.size, returning };
}
const cache = new LRUCache(3);
const days = ["Mon", "Tue", "Mon", "Wed", "Thu", "Mon", "Tue"];
for (const day of days) {
const start = performance.now();
const r = cache.get(day, () => report(makeOrders(200000)));
console.log(`${day}: ${r.customers} customers, ${r.returning} returning (${(performance.now() - start).toFixed(1)} ms)`);
}
console.log(`cache: ${cache.map.size} days kept, ${cache.hits} hits, ${cache.misses} misses`);
console.log("kept:", [...cache.map.keys()]);
Real output:
Mon: 100000 customers, 100000 returning (105.5 ms)
Tue: 100000 customers, 100000 returning (83.8 ms)
Mon: 100000 customers, 100000 returning (0.0 ms)
Wed: 100000 customers, 100000 returning (103.2 ms)
Thu: 100000 customers, 100000 returning (52.8 ms)
Mon: 100000 customers, 100000 returning (0.0 ms)
Tue: 100000 customers, 100000 returning (63.7 ms)
cache: 3 days kept, 2 hits, 5 misses
kept: [ 'Thu', 'Mon', 'Tue' ]
What to notice:
reportis O(n): one pass to count with aMap, one pass over the counts.- The second "Mon" is a cache hit (0.0 ms). Because reading Mon moved it to the newest end, it survived when Thu was added; Tue (the least recently used) was evicted, so the last "Tue" is a miss and is computed again.
- The cache never holds more than 3 reports, however many days you ask for.
Extensions: add a size getter; add a time limit ("forget reports older than 10 minutes") with Date.now(); write node:test tests (episode 30) for the hit/miss counts.
Practice
- Time
includesagainstSet.hasfor 20,000 items (each looked up once). - Find all duplicate order ids in one pass (O(n)).
- Count orders per customer with a
Map, and print the customer with the most orders. - Two dishes must add up exactly to a budget. Write it in O(n) with a
Set. - Fix a leaky
setIntervalwithclearInterval. - What is the Big O of this, and how would you fix it?
orders.filter(o => vipPhones.includes(o.phone))
Solutions
1. includes vs Set.has
const items = [];
for (let i = 0; i < 20000; i++) items.push("item" + i);
const set = new Set(items);
console.time("includes");
let found = 0;
for (let i = 0; i < 20000; i++) if (items.includes("item" + i)) found++;
console.timeEnd("includes");
console.time("set.has");
found = 0;
for (let i = 0; i < 20000; i++) if (set.has("item" + i)) found++;
console.timeEnd("set.has");
console.log(found, "found");
includes: 1.519s
set.has: 18.434ms
20000 found
(Building the Set once is one O(n) pass; it's outside the timers here, but even counted in, it's tiny.)
2. Duplicates in one pass
const orderIds = ["A1", "B7", "A1", "C3", "B7", "D9"];
function duplicates(ids) {
const seen = new Set();
const dups = new Set();
for (const id of ids) {
if (seen.has(id)) dups.add(id);
else seen.add(id);
}
return [...dups];
}
console.log(duplicates(orderIds));
[ 'A1', 'B7' ]
dups is a Set too, so an id that appears three times is listed only once.
3. Orders per customer
const orders = ["9812", "9845", "9812", "9877", "9812", "9845"];
function ordersPerCustomer(phones) {
const count = new Map();
for (const phone of phones) {
count.set(phone, (count.get(phone) ?? 0) + 1);
}
return count;
}
console.log(ordersPerCustomer(orders));
Map(3) { '9812' => 3, '9845' => 2, '9877' => 1 }
For the top customer, loop once over count (for (const [phone, n] of count)) and keep the largest n: still O(n).
4. Two dishes for a budget
function pairForBudget(prices, budget) {
const seen = new Set();
for (const p of prices) {
if (seen.has(budget - p)) return [budget - p, p];
seen.add(p);
}
return null;
}
console.log(pairForBudget([120, 40, 90, 60, 30], 150));
console.log(pairForBudget([120, 40], 100));
[ 90, 60 ]
null
For each price, ask "have I already seen the price that completes the budget?". Two nested loops would be O(n²); the Set makes it O(n).
5. Leaky timer: see "The timer leak" above: const timer = setInterval(...), return () => clearInterval(timer), and call it when the screen closes.
6. filter is a loop over orders (n), and includes is a loop over vipPhones (m) inside it: O(n × m). Fix: const vip = new Set(vipPhones); then orders.filter(o => vip.has(o.phone)): O(n + m).
Quick quiz
We have a million ids, and must check whether "C42" is in there, a million times. Which is fastest?
const ids = [/* 1,000,000 ids */];
// check if "C42" is in there, a million times
- A.
ids.includes("C42")each time - B.
ids.indexOf("C42")each time - C.
new Set(ids)once, thenset.has("C42") - D.
ids.filter(id => id === "C42")each time
Answer
C. includes, indexOf and filter all read the array from the start: O(n) per check, a million times, so O(n²) in total (and filter even builds a new array every time). Building a Set once costs one O(n) pass; after that, every has is O(1).
Interview questions
- What is Big O notation? A way to describe how the work (or memory) of an algorithm grows as the input grows, ignoring constants and machine speed. O(1) constant, O(n) linear, O(n²) quadratic.
- Why is
array.includesinside a loop slow?includesis itself a loop over the array, so the total is O(n²). Use aSetfor O(1) lookups. - What is the time complexity of
Set.hasandMap.get? O(1) on average: they're hash tables. - How do you measure the speed of code in Node?
console.time/console.timeEnd, orperformance.now()before and after; for real apps, a profiler (node --prof, Chrome DevTools vianode --inspect). - What does "blocking the event loop" mean? Long synchronous code occupies Node's single JavaScript thread, so timers, I/O callbacks and new requests can't run until it ends. Fix with a better algorithm, chunking (
setImmediate), orworker_threads. - How does garbage collection work in JavaScript? The engine frees objects that are no longer reachable from the program's roots (globals, the current stack, closures, timers...). V8 uses a generational collector: quick "scavenges" for young objects, and "Mark-Compact" for old ones.
- What is a memory leak in a garbage-collected language? Memory that stays reachable by mistake (a growing cache or array, an uncleared timer, an unremoved listener, a closure holding big data), so the GC can never free it.
- How would you detect a leak? Watch
process.memoryUsage().heapUsed(or a monitoring graph) over time: a sawtooth is fine, a line that only rises is a leak. Then take heap snapshots (Chrome DevTools,node --inspect) and compare them. - What is the difference between
heapUsedandrss?heapUsedis the JavaScript heap in use;rssis all memory of the process (heap, code, buffers, native memory). - How do you write a bounded or LRU cache in JavaScript? Use a
Map(it keeps insertion order). On insert, ifsize > limit, deletemap.keys().next().value(the oldest). For LRU, on every read delete the key and set it again to move it to the newest end. - Can you catch "JavaScript heap out of memory"? No: it's a fatal error that kills the process. Fix the leak, or (only if the app really needs it) raise the limit with
--max-old-space-size. - Is
forfaster thanforEach? Slightly, sometimes, but it rarely matters. Choosing the right algorithm and data structure matters far more.
Glossary
| Term | Meaning | Example |
|---|---|---|
| Performance | how fast code runs and how much memory it uses | 40,000 orders in 9 ms |
| Algorithm | the step-by-step method a piece of code uses | "check every earlier phone" vs "look it up in a Set" |
| Data structure | a way of organising data in memory | array, Set, Map |
| Big O | how the work grows as the input grows, ignoring exact times | O(n²) |
| O(1), constant time | the same work whatever the size | set.has(x) |
| O(n), linear time | work grows in step with the data | one loop over the orders |
| O(n²), quadratic time | work grows with the square of the data: 10× data, 100× work | includes inside a loop |
| O(log n), logarithmic | work grows very slowly: double the data, one more step | binary search |
n |
the size of the input | number of orders |
Set |
a collection of unique values with O(1) has/add/delete |
new Set() |
Map |
key → value pairs with O(1) get/set; remembers insertion order |
count.set(phone, n) |
| Hash table | the structure behind Set/Map: a value is turned into a number that says where it's stored |
the flat number on a letter box |
array.includes(x) |
true if x is in the array; checks items one by one, O(n) |
seen.includes(phone) |
console.time(label) / console.timeEnd(label) |
start / stop a named stopwatch and print the time | console.time("orders") |
performance.now() |
milliseconds since the program started, with decimals | const start = performance.now() |
Date.now() |
milliseconds since 1 January 1970, whole numbers | timer delays |
| Warning | a message on stderr that doesn't stop the program | No such label 'order' |
| Benchmark | a measurement of how fast code runs | timing 1,000 / 10,000 / 40,000 orders |
| Thread | one line of execution; Node runs your JavaScript on one | the one cook |
| Event loop | Node's loop that runs queued callbacks when the current code finishes | timers, requests |
| Blocking | code that occupies the thread so nothing else can run | the slow daily report |
| Synchronous | runs to the end before the next line | a for loop |
setImmediate(fn) |
run fn on the next turn of the event loop |
chunked work |
worker_threads |
Node module to run JavaScript on extra threads | a second cook |
| Memory | where a running program keeps its data | variables, objects |
| Heap | the memory area where objects, arrays and strings live | heapUsed |
| Garbage | objects nothing points to any more | old menus after cache.delete |
| Garbage collector (GC) | the part of the engine that frees garbage automatically | Mark-Compact |
| Reachable | can still be reached from the program (a variable, a Map, a closure, a timer) | menus inside cache |
| Reference | a variable or property that points to an object | cache.set(id, menu) |
| Memory leak | memory kept by a forgotten reference, so it can't be freed | a cache with no limit |
process.memoryUsage() |
returns rss, heapTotal, heapUsed, external in bytes |
.heapUsed / 1024 / 1024 |
heapUsed |
heap memory in use right now (including uncollected garbage) | 88 MB |
rss |
resident set size: all memory used by the process | 41.9 MB |
| MB (megabyte) | 1024 × 1024 bytes | bytes / 1024 / 1024 |
--max-old-space-size |
Node flag that sets the heap limit in MB | node --max-old-space-size=40 main.js |
| Heap out of memory | fatal error when the heap limit is reached; can't be caught | FATAL ERROR: Reached heap limit ... |
| Mark-Compact | V8's full garbage collection of old objects | in the "Last few GCs" log |
| Cache | stored results kept to reuse later | cache Map of menus |
| Bounded cache | a cache with a maximum size | LIMIT = 1000 |
| Eviction | removing an entry from a full cache | cache.delete(oldestKey) |
| FIFO | first in, first out: evict the oldest added | the episode's cache |
| LRU | least recently used: evict the entry not read for longest | the mini project |
| Insertion order | Map and Set iterate in the order keys were added |
cache.keys().next().value |
| Sawtooth | memory going up and down as the GC runs: healthy | the bounded cache |
setInterval / clearInterval |
run a function every N ms / stop it | const timer = setInterval(...) |
| Event listener | a function registered with on to run when an event happens |
kitchen.on("order", show) |
emitter.off(event, fn) |
remove a listener (must be the same function) | kitchen.off("order", show) |
MaxListenersExceededWarning |
Node's warning that more than 10 listeners were added to one event | possible listener leak |
| Closure | a function that remembers the variables around it (episode 10) | the timer callback holding orders |
| V8 | the JavaScript engine inside Node and Chrome | optimises hot functions |
| Profiler | a tool that shows where a program spends its time | node --inspect + DevTools |
Summary
- Correct code can still be slow.
includesinside a loop is O(n²): 10× the data, ~100× the time (1,000 orders in ~1 ms, 40,000 in ~1 s). - Big O describes how work grows: O(1) constant, O(n) linear, O(n²) quadratic. Fix the Big O before anything else.
- A
Set(orMap) jumps straight to the answer in O(1): 40,000 orders in ~10 ms, a million in about half a second. - Measure, don't guess:
console.time/timeEnd(labels must match exactly, or you getWarning: No such label), andperformance.now(). - Node runs your code on one thread: slow synchronous code blocks the event loop, so a 10 ms timer fired ~1 s late. Fix the algorithm; for truly heavy work, chunk it or use
worker_threads. - Objects live on the heap; the garbage collector frees only what nothing points to, when it chooses (
process.memoryUsage().heapUsedshows a healthy sawtooth). - A cache with no limit is a memory leak: ~80 MB more every round, until
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory. Give caches a limit (aMapkeeps insertion order, so delete the oldest key). - Other leaks: uncleared
setInterval, listeners never removed, closures holding big data, global arrays that only grow.
Next episode: TypeScript basics. Our tests catch bugs when the code runs; next, we catch a whole class of bugs before the code even runs.