Node.js `perf_hooks` Core Module (npm `perf_hooks` placeholder)
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
-
TypeError: (0 , perf_hooks_1.performance) is not a function
cause Attempting to use `performance` from the abandoned `perf_hooks` npm package (version 0.0.1) which exports nothing, instead of the Node.js core module.fixChange your import statement from `import { performance } from 'perf_hooks';` to `import { performance } from 'node:perf_hooks';` (or `require`). Ensure the npm package is uninstalled. -
Error: Cannot find module 'perf_hooks'
cause This error can occur if you've incorrectly removed the `perf_hooks` npm package but your code still tries to import it, or if you're trying to use `perf_hooks` in a non-Node.js environment (e.g., a browser without a polyfill/bundler setup).fixIf running in Node.js, ensure you are importing from the core module using `node:perf_hooks`. If this error appears after uninstalling the npm package, it indicates a lingering reference. If running in a browser, `perf_hooks` is a Node.js-specific API and will not work without a compatibility layer or polyfill.
Warnings
- breaking The npm package `perf_hooks` (version 0.0.1) is an abandoned placeholder and provides no functional APIs. Installing and attempting to use this package will result in runtime errors as its exports are empty or non-existent.
- gotcha Using `require('perf_hooks')` or `import 'perf_hooks'` without the `node:` prefix can lead to confusion and potentially load the abandoned npm placeholder package instead of the core Node.js module, particularly in environments with complex module resolution or non-standard bundler configurations.
- gotcha The `perf_hooks` core module's global `performance` object is similar to `window.performance` in browsers but includes Node.js-specific extensions like `timerify()` and `eventLoopUtilization()` which are not available in web environments.
Install
-
npm install perf_hooks -
yarn add perf_hooks -
pnpm add perf_hooks
Imports
- performance
import { performance } from 'perf_hooks';import { performance } from 'node:perf_hooks'; - PerformanceObserver
const { PerformanceObserver } = require('perf_hooks');const { PerformanceObserver } = require('node:perf_hooks'); - timerify
const { timerify } = require('perf_hooks'); // Incorrect for placeholderimport { timerify } from 'node:perf_hooks';
Quickstart
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);