Course contentsShow
← JavaScript
Lesson 1924 of 2,72340. PerformanceFree lesson

Node.js Performance Hooks

Using the perf_hooks module to measure server-side performance with timers and observers.

Node.js Performance Hooks

You'll learn how to use Node.js's built-in perf_hooks module to measure server-side performance with timers and observers, just like you used the browser's Performance API.

What Are Performance Hooks?

Node.js provides the perf_hooks module, which brings performance measurement capabilities similar to what you've used in browsers—but optimized for server-side code. Think of it as your backstage timing equipment that measures how long operations take without slowing down the show.

The perf_hooks Module

The module exports a performance object that works much like the browser version you already know:

const { performance, PerformanceObserver } = require('perf_hooks');

// High-resolution timing
const start = performance.now();
// ... some operation
const duration = performance.now() - start;

// Mark-and-measure API
performance.mark('query-start');
await database.query('SELECT * FROM users');
performance.mark('query-end');
performance.measure('db-query', 'query-start', 'query-end');

Using PerformanceObserver

Instead of polling for measurements, you can observe them as they happen:

const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach(entry => {
    console.log(`${entry.name}: ${entry.duration}ms`);
  });
});

obs.observe({ entryTypes: ['measure'] });

Now every performance.measure() call automatically triggers your observer—perfect for centralizing performance logging in APIs or background jobs.

Why This Matters in Node.js

Unlike browsers, Node.js applications run continuously, handling thousands of requests. Performance hooks let you track database queries, file operations, API calls, and more in production without external dependencies.

Key Takeaway: Node.js perf_hooks provides performance.now(), marks, measures, and observers for measuring server-side performance using the same mental model you learned for browsers.