Lab 08: Performance Profiling
Step 1: perf_hooks — PerformanceObserver
perf_hooks — PerformanceObserver// file: perf-observer.js
const { performance, PerformanceObserver } = require('perf_hooks');
// Set up observer BEFORE creating marks
const obs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log(`${entry.name}: ${entry.duration.toFixed(3)}ms`);
}
obs.disconnect();
});
obs.observe({ entryTypes: ['measure'] });
// Benchmark: sort 100k elements
performance.mark('sort-start');
const arr = Array.from({ length: 100_000 }, () => Math.random());
arr.sort((a, b) => a - b);
performance.mark('sort-end');
performance.measure('array-sort-100k', 'sort-start', 'sort-end');
console.log('Sorted', arr.length, 'elements');Step 2: Detailed Timing with performance.now()
performance.now()Step 3: V8 Profiler with --prof
--profStep 4: node:trace_events — Event Loop Tracing
node:trace_events — Event Loop TracingStep 5: Heap Profiling with --heap-prof
--heap-profStep 6: autocannon Load Testing
Step 7: clinic.js — Production Profiling Suite
Step 8: Capstone — Complete Performance Analysis Tool
Summary
Tool
Command
Use Case
Last updated
