{"library":"perf_hooks","title":"Node.js `perf_hooks` Core Module (npm `perf_hooks` placeholder)","description":"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.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install perf_hooks"],"cli":null},"imports":["import { performance } from 'node:perf_hooks';","const { PerformanceObserver } = require('node:perf_hooks');","import { timerify } from 'node:perf_hooks';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { performance, PerformanceObserver } from 'node:perf_hooks';\n\n// Create a PerformanceObserver to collect and report performance entries.\nconst obs = new PerformanceObserver((list) => {\n  const entries = list.getEntries();\n  entries.forEach((entry) => {\n    console.log(`Entry: ${entry.name}, Type: ${entry.entryType}, Duration: ${entry.duration.toFixed(2)}ms`);\n  });\n  // Clear marks and measures after processing if they are no longer needed\n  performance.clearMarks();\n  performance.clearMeasures();\n});\n\n// Start observing 'mark' and 'measure' events.\nobs.observe({ entryTypes: ['mark', 'measure'] });\n\n// Simulate an asynchronous operation with performance marks.\nasync function simulateWork() {\n  performance.mark('startWork');\n  await new Promise(resolve => setTimeout(resolve, Math.random() * 500)); // Simulate async task\n  performance.mark('endWork');\n  performance.measure('Total Work Time', 'startWork', 'endWork');\n\n  performance.mark('startAnotherTask');\n  let sum = 0;\n  for (let i = 0; i < 1_000_000; i++) {\n    sum += i; // CPU-bound task\n  }\n  performance.mark('endAnotherTask');\n  performance.measure('CPU Bound Task', 'startAnotherTask', 'endAnotherTask');\n  console.log('Simulated work complete. Sum:', sum);\n}\n\nsimulateWork();\n\n// Example of Event Loop Utilization\nimport { eventLoopUtilization } from 'node:perf_hooks';\n\nconst eluBefore = eventLoopUtilization();\nsetTimeout(() => {\n  const eluAfter = eventLoopUtilization(eluBefore);\n  console.log(`Event Loop Utilization: ${eluAfter.utilization.toFixed(4)}`);\n}, 1000);","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}