Node.js v18.12.1 documentation


Table of contents

Performance measurement APIs性能度量API#

Stability: 2 - Stable

Source Code: lib/perf_hooks.js

This module provides an implementation of a subset of the W3C Web Performance APIs as well as additional APIs for Node.js-specific performance measurements.该模块提供了W3C Web性能API的一个子集的实现,以及用于Node.js特定性能度量的其他API。

Node.js supports the following Web Performance APIs:Node.js支持以下Web性能API

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

const obs = new PerformanceObserver((items) => {
console.log(items.getEntries()[0].duration);
performance.clearMarks();
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');
doSomeLongRunningProcess(() => {
performance.measure('A to Now', 'A');

performance.mark('B');
performance.measure('A to B', 'A', 'B');
});