Back to Blog
Node.jsperf_hooksPerformanceMonitoring

Node.js perf_hooks Module Guide

Master Node.js perf_hooks for precise performance measurement, timing, and resource monitoring.

B
Bootspring Team
Engineering
March 8, 2019
6 min read

The perf_hooks module provides precise performance timing APIs similar to browser Performance APIs. Here's how to use it.

Basic Timing#

Loading code block...

Performance Marks and Measures#

Loading code block...

PerformanceObserver#

Loading code block...

Timing Functions#

Loading code block...

HTTP Server Timing#

Loading code block...

Histogram for Statistics#

Loading code block...

Resource Timing#

Loading code block...

Custom Performance Entries#

Loading code block...

Performance Timeline#

Loading code block...

Profiling Middleware#

Loading code block...

Benchmark Utility#

Loading code block...

Best Practices#

Measurement: ✓ Use marks and measures ✓ Name entries descriptively ✓ Clear entries after collection ✓ Use PerformanceObserver for async Accuracy: ✓ Warmup before benchmarking ✓ Multiple iterations ✓ Calculate percentiles ✓ Account for GC pauses Production: ✓ Sample measurements ✓ Aggregate statistics ✓ Monitor percentiles (p95, p99) ✓ Set alerting thresholds Avoid: ✗ Too many performance marks ✗ Forgetting to clear entries ✗ Blocking with analysis ✗ Ignoring outliers

Conclusion#

The perf_hooks module provides powerful tools for precise performance measurement in Node.js. Use marks and measures for timing code sections, PerformanceObserver for async monitoring, and histograms for statistical analysis. Implement timing middleware for HTTP servers and build benchmark utilities for comparing implementations. Remember to clear entries to prevent memory growth and sample in production to minimize overhead.

Share this article

Help spread the word about Bootspring

Related articles