Node.js › Core Node.js Modules

What does the node:perf_hooks module provide, and how would you use it to measure precise execution time of a code block?

Advanced

Answer

perf_hooks provides high-resolution timing via performance.now() (unaffected by system clock adjustments, unlike Date.now()) and PerformanceObserver for capturing marks and measures -- useful for precisely benchmarking operations without clock-drift imprecision.
performance.mark('start');
expensiveOperation();
performance.mark('end');
performance.measure('duration', 'start', 'end');
Real-world example An API wraps a latency-sensitive query with performance.now() timestamps for its metrics system, using perf_hooks since Date.now()'s lower resolution would be less reliable for sub-millisecond differences.

Common follow-ups: Why is performance.now() more reliable for timing than Date.now()?;How does PerformanceObserver compare to simply subtracting two performance.now() timestamps?