Lesson 2.2:
Profiling & Memory Leaks
Finding the Bottleneck
Performance optimization starts with measurement. In 2026, we use native profiling flags and the Chrome DevTools integration to identify hot functions.
Generating Flamegraphs
A flamegraph visualizes where your CPU time is spent. Run Node.js with the profiler to generate reports.
$ node --prof app.js
# After some load...
$ node --prof-process isolate-0xnnnnnnnnnnnn-v8.log > processed.txt
Memory Leak Hunting
Closures and large global caches are the primary sources of memory leaks. Use Heap Snapshots to find objects that refuse to be Garbage Collected.
import { writeHeapSnapshot } from 'node:v8';
writeHeapSnapshot(); // Take a snapshot for analysis
Check Your Knowledge
Which visualization helps identify slow functions in the stack?