The perf_hooks module provides APIs for performance measurement and timing. Here's how to use it.
Basic Performance Measurement
Performance Observer
Function Timing
Custom Metrics
HTTP Server Timing
Database Query Timing
Histogram
Event Loop Monitoring
Resource Usage Tracking
Express Middleware
Best Practices
Measurement:
✓ Use marks and measures
✓ Clean up entries after use
✓ Use observers for async
✓ Include context in marks
Monitoring:
✓ Track event loop delay
✓ Monitor memory alongside timing
✓ Set up alerting thresholds
✓ Log slow operations
Performance:
✓ Minimize observer overhead
✓ Sample in production
✓ Aggregate metrics
✓ Use histograms for distribution
Avoid:
✗ Too many marks without cleanup
✗ Synchronous getEntries in hot paths
✗ Ignoring memory leaks
✗ Excessive logging overhead
Conclusion
The perf_hooks module provides powerful APIs for performance measurement in Node.js. Use marks and measures for timing, observers for async operations, and histograms for distribution analysis. Monitor event loop delay to detect blocking operations and combine with memory metrics for comprehensive profiling.