Node.js
/Advanced
Performance Profiling
Definition
The process of analyzing a software program's execution to determine its time complexity, memory usage, and the frequency/duration of function calls.
Explain Like I'm New
Using an X-Ray machine on your code. If your API takes 5 seconds to load, you don't guess what's wrong. You run a Profiler. It outputs a chart showing: 'Database Query took 0.1s. Math calculation took 4.8s. Formatting JSON took 0.1s.' You immediately know exactly which line of code to fix.
Real World Example
Taking a 'Heap Snapshot' using Chrome DevTools. You take a snapshot at 1:00 PM (100MB RAM used). You take another at 2:00 PM (300MB RAM used). You compare the snapshots, and the profiler highlights exactly which JavaScript Objects are accumulating and causing the memory leak.
Common Use Cases
- •Diagnosing memory leaks
- •Identifying Event Loop blocking code
Terminal Output
bash / terminal
// --- HOW TO PROFILE --- //
/*
1. Start your app with the inspect flag:
node --inspect server.js
2. Open Google Chrome browser.
3. Navigate to: chrome://inspect
4. Click "Open dedicated DevTools for Node"
5. Go to the "Memory" tab and click "Take Heap Snapshot"
6. Send 10,000 fake requests to your server using a tool like Artillery or JMeter.
7. Take a second "Heap Snapshot".
8. Compare the two snapshots. The DevTools will show you EXACTLY which variables
grew in size and didn't get Garbage Collected.
*/
console.log("Profiling is the difference between blindly guessing performance issues and scientifically proving them.");
Interview Questions
basic
- Can you use Google Chrome to inspect a Node.js server backend?
intermediate
- What is a 'Flame Graph'?