typescriptadvanced
Debug with Inspector Protocol
Programmatic debugging with Node.js inspector module for heap snapshots, CPU profiling, and breakpoints.
typescriptPress ⌘/Ctrl + Shift + C to copy
import inspector from 'inspector';
import fs from 'fs';
// CPU Profiling
async function profileCPU(fn: () => void | Promise<void>, outputFile = 'profile.cpuprofile') {
const session = new inspector.Session();
session.connect();
session.post('Profiler.enable');
session.post('Profiler.start');
await fn();
return new Promise<void>((resolve) => {
session.post('Profiler.stop', (err, { profile }) => {
if (!err && profile) {
fs.writeFileSync(outputFile, JSON.stringify(profile));
console.log(`CPU profile saved to ${outputFile}`);
console.log('Open in Chrome DevTools: Performance > Load Profile');
}
session.disconnect();
resolve();
});
});
}
// Heap snapshot
async function takeHeapSnapshot(outputFile = 'heap.heapsnapshot') {
const session = new inspector.Session();
session.connect();
const chunks: string[] = [];
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
chunks.push(m.params.chunk);
});
return new Promise<void>((resolve) => {
session.post('HeapProfiler.takeHeapSnapshot', undefined, () => {
fs.writeFileSync(outputFile, chunks.join(''));
console.log(`Heap snapshot saved to ${outputFile}`);
session.disconnect();
resolve();
});
});
}
// Memory usage tracking
function trackMemory(intervalMs = 5000) {
const baseline = process.memoryUsage();
console.log('Memory baseline:', formatMemory(baseline));
const timer = setInterval(() => {
const current = process.memoryUsage();
console.log('Memory:', formatMemory(current), '| Growth:', {
rss: `+${((current.rss - baseline.rss) / 1e6).toFixed(1)}MB`,
heap: `+${((current.heapUsed - baseline.heapUsed) / 1e6).toFixed(1)}MB`,
});
}, intervalMs);
return () => clearInterval(timer);
}
function formatMemory(m: NodeJS.MemoryUsage) {
return {
rss: `${(m.rss / 1e6).toFixed(1)}MB`,
heapUsed: `${(m.heapUsed / 1e6).toFixed(1)}MB`,
heapTotal: `${(m.heapTotal / 1e6).toFixed(1)}MB`,
external: `${(m.external / 1e6).toFixed(1)}MB`,
};
}
// Usage
async function main() {
const stopTracking = trackMemory(2000);
await profileCPU(() => {
let sum = 0;
for (let i = 0; i < 1e7; i++) sum += Math.sqrt(i);
console.log('CPU work done:', sum.toFixed(2));
});
await takeHeapSnapshot();
stopTracking();
}
main();Use Cases
- Production performance profiling
- Memory leak detection
- CPU bottleneck analysis
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Performance Measurement
Measure execution time with performance.now(), Performance API marks, measures, and PerformanceObserver.
Best for: Function execution profiling
#nodejs#performance
typescriptadvanced
Node.js Worker Threads for Parallel Processing
Use Worker Threads to run CPU-intensive tasks in parallel without blocking the event loop.
Best for: CPU-intensive data processing without blocking
#nodejs#worker-threads
typescriptintermediate
Node.js Stream Pipeline with Transform
Build efficient data processing pipelines using Node.js streams for large file handling.
Best for: Processing large CSV files without loading into memory
#nodejs#streams
typescriptintermediate
TypeScript Typed Event Emitter
Create type-safe event emitters in Node.js with full TypeScript support and autocomplete.
Best for: Type-safe pub/sub communication between modules
#nodejs#events