Node.js `perf_hooks` Core Module (npm `perf_hooks` placeholder)

0.0.1 · abandoned · verified Sun Apr 19

The `perf_hooks` module is a fundamental, built-in Node.js core module that provides performance measurement APIs based on the W3C User Timing and Performance Timeline specifications. It enables developers to instrument and observe Node.js application behavior with high-resolution timers, performance marks, measures, and observers. Key APIs include `performance.now()`, `performance.mark()`, `performance.measure()`, `PerformanceObserver`, and `eventLoopUtilization()` for monitoring event loop delays. This functionality is part of Node.js itself and does not require a separate npm installation; it is stable across all supported Node.js versions (currently 18.x LTS, 20.x LTS, 22.x LTS, with new versions released regularly). Critically, the npm package `perf_hooks` (version 0.0.1) mentioned in the metadata is a long-abandoned placeholder, last published nine years ago. Despite having high weekly downloads, it offers no actual functionality and should *never* be installed or used. Users seeking performance tooling should rely exclusively on the `node:perf_hooks` core module.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `performance.mark()`, `performance.measure()`, and `PerformanceObserver` to track the duration of asynchronous and synchronous operations, as well as `eventLoopUtilization()` for Node.js performance monitoring.

import { performance, PerformanceObserver } from 'node:perf_hooks';

// Create a PerformanceObserver to collect and report performance entries.
const obs = new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    console.log(`Entry: ${entry.name}, Type: ${entry.entryType}, Duration: ${entry.duration.toFixed(2)}ms`);
  });
  // Clear marks and measures after processing if they are no longer needed
  performance.clearMarks();
  performance.clearMeasures();
});

// Start observing 'mark' and 'measure' events.
obs.observe({ entryTypes: ['mark', 'measure'] });

// Simulate an asynchronous operation with performance marks.
async function simulateWork() {
  performance.mark('startWork');
  await new Promise(resolve => setTimeout(resolve, Math.random() * 500)); // Simulate async task
  performance.mark('endWork');
  performance.measure('Total Work Time', 'startWork', 'endWork');

  performance.mark('startAnotherTask');
  let sum = 0;
  for (let i = 0; i < 1_000_000; i++) {
    sum += i; // CPU-bound task
  }
  performance.mark('endAnotherTask');
  performance.measure('CPU Bound Task', 'startAnotherTask', 'endAnotherTask');
  console.log('Simulated work complete. Sum:', sum);
}

simulateWork();

// Example of Event Loop Utilization
import { eventLoopUtilization } from 'node:perf_hooks';

const eluBefore = eventLoopUtilization();
setTimeout(() => {
  const eluAfter = eventLoopUtilization(eluBefore);
  console.log(`Event Loop Utilization: ${eluAfter.utilization.toFixed(4)}`);
}, 1000);

view raw JSON →