{ "type": "module", "source": "doc/api/perf_hooks.md", "modules": [ { "textRaw": "Performance measurement APIs", "name": "performance_measurement_apis", "introduced_in": "v8.5.0", "stability": 2, "stabilityText": "Stable", "desc": "

Source Code: lib/perf_hooks.js

\n

This module provides an implementation of a subset of the W3C\nWeb Performance APIs as well as additional APIs for\nNode.js-specific performance measurements.

\n

Node.js supports the following Web Performance APIs:

\n\n
const { PerformanceObserver, performance } = require('perf_hooks');\n\nconst obs = new PerformanceObserver((items) => {\n  console.log(items.getEntries()[0].duration);\n  performance.clearMarks();\n});\nobs.observe({ type: 'measure' });\nperformance.measure('Start to Now');\n\nperformance.mark('A');\ndoSomeLongRunningProcess(() => {\n  performance.measure('A to Now', 'A');\n\n  performance.mark('B');\n  performance.measure('A to B', 'A', 'B');\n});\n
", "properties": [ { "textRaw": "`perf_hooks.performance`", "name": "performance", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

An object that can be used to collect performance metrics from the current\nNode.js instance. It is similar to window.performance in browsers.

", "methods": [ { "textRaw": "`performance.clearMarks([name])`", "type": "method", "name": "clearMarks", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string" } ] } ], "desc": "

If name is not provided, removes all PerformanceMark objects from the\nPerformance Timeline. If name is provided, removes only the named mark.

" }, { "textRaw": "`performance.eventLoopUtilization([utilization1[, utilization2]])`", "type": "method", "name": "eventLoopUtilization", "meta": { "added": [ "v14.10.0", "v12.19.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns {Object}", "name": "return", "type": "Object", "options": [ { "textRaw": "`idle` {number}", "name": "idle", "type": "number" }, { "textRaw": "`active` {number}", "name": "active", "type": "number" }, { "textRaw": "`utilization` {number}", "name": "utilization", "type": "number" } ] }, "params": [ { "textRaw": "`utilization1` {Object} The result of a previous call to `eventLoopUtilization()`.", "name": "utilization1", "type": "Object", "desc": "The result of a previous call to `eventLoopUtilization()`." }, { "textRaw": "`utilization2` {Object} The result of a previous call to `eventLoopUtilization()` prior to `utilization1`.", "name": "utilization2", "type": "Object", "desc": "The result of a previous call to `eventLoopUtilization()` prior to `utilization1`." } ] } ], "desc": "

The eventLoopUtilization() method returns an object that contains the\ncumulative duration of time the event loop has been both idle and active as a\nhigh resolution milliseconds timer. The utilization value is the calculated\nEvent Loop Utilization (ELU).

\n

If bootstrapping has not yet finished on the main thread the properties have\nthe value of 0. The ELU is immediately available on Worker threads since\nbootstrap happens within the event loop.

\n

Both utilization1 and utilization2 are optional parameters.

\n

If utilization1 is passed, then the delta between the current call's active\nand idle times, as well as the corresponding utilization value are\ncalculated and returned (similar to process.hrtime()).

\n

If utilization1 and utilization2 are both passed, then the delta is\ncalculated between the two arguments. This is a convenience option because,\nunlike process.hrtime(), calculating the ELU is more complex than a\nsingle subtraction.

\n

ELU is similar to CPU utilization, except that it only measures event loop\nstatistics and not CPU usage. It represents the percentage of time the event\nloop has spent outside the event loop's event provider (e.g. epoll_wait).\nNo other CPU idle time is taken into consideration. The following is an example\nof how a mostly idle process will have a high ELU.

\n
'use strict';\nconst { eventLoopUtilization } = require('perf_hooks').performance;\nconst { spawnSync } = require('child_process');\n\nsetImmediate(() => {\n  const elu = eventLoopUtilization();\n  spawnSync('sleep', ['5']);\n  console.log(eventLoopUtilization(elu).utilization);\n});\n
\n

Although the CPU is mostly idle while running this script, the value of\nutilization is 1. This is because the call to\nchild_process.spawnSync() blocks the event loop from proceeding.

\n

Passing in a user-defined object instead of the result of a previous call to\neventLoopUtilization() will lead to undefined behavior. The return values\nare not guaranteed to reflect any correct state of the event loop.

" }, { "textRaw": "`performance.mark([name[, options]])`", "type": "method", "name": "mark", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Updated to conform to the User Timing Level 3 specification." } ] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`detail` {any} Additional optional detail to include with the mark.", "name": "detail", "type": "any", "desc": "Additional optional detail to include with the mark." }, { "textRaw": "`startTime` {number} An optional timestamp to be used as the mark time. **Defaults**: `performance.now()`.", "name": "startTime", "type": "number", "desc": "An optional timestamp to be used as the mark time. **Defaults**: `performance.now()`." } ] } ] } ], "desc": "

Creates a new PerformanceMark entry in the Performance Timeline. A\nPerformanceMark is a subclass of PerformanceEntry whose\nperformanceEntry.entryType is always 'mark', and whose\nperformanceEntry.duration is always 0. Performance marks are used\nto mark specific significant moments in the Performance Timeline.

" }, { "textRaw": "`performance.measure(name[, startMarkOrOptions[, endMark]])`", "type": "method", "name": "measure", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Updated to conform to the User Timing Level 3 specification." }, { "version": [ "v13.13.0", "v12.16.3" ], "pr-url": "https://github.com/nodejs/node/pull/32651", "description": "Make `startMark` and `endMark` parameters optional." } ] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string" }, { "textRaw": "`startMarkOrOptions` {string|Object} Optional.", "name": "startMarkOrOptions", "type": "string|Object", "desc": "Optional.", "options": [ { "textRaw": "`detail` {any} Additional optional detail to include with the measure.", "name": "detail", "type": "any", "desc": "Additional optional detail to include with the measure." }, { "textRaw": "`duration` {number} Duration between start and end times.", "name": "duration", "type": "number", "desc": "Duration between start and end times." }, { "textRaw": "`end` {number|string} Timestamp to be used as the end time, or a string identifying a previously recorded mark.", "name": "end", "type": "number|string", "desc": "Timestamp to be used as the end time, or a string identifying a previously recorded mark." }, { "textRaw": "`start` {number|string} Timestamp to be used as the start time, or a string identifying a previously recorded mark.", "name": "start", "type": "number|string", "desc": "Timestamp to be used as the start time, or a string identifying a previously recorded mark." } ] }, { "textRaw": "`endMark` {string} Optional. Must be omitted if `startMarkOrOptions` is an {Object}.", "name": "endMark", "type": "string", "desc": "Optional. Must be omitted if `startMarkOrOptions` is an {Object}." } ] } ], "desc": "

Creates a new PerformanceMeasure entry in the Performance Timeline. A\nPerformanceMeasure is a subclass of PerformanceEntry whose\nperformanceEntry.entryType is always 'measure', and whose\nperformanceEntry.duration measures the number of milliseconds elapsed since\nstartMark and endMark.

\n

The startMark argument may identify any existing PerformanceMark in the\nPerformance Timeline, or may identify any of the timestamp properties\nprovided by the PerformanceNodeTiming class. If the named startMark does\nnot exist, an error is thrown.

\n

The optional endMark argument must identify any existing PerformanceMark\nin the Performance Timeline or any of the timestamp properties provided by the\nPerformanceNodeTiming class. endMark will be performance.now()\nif no parameter is passed, otherwise if the named endMark does not exist, an\nerror will be thrown.

" }, { "textRaw": "`performance.now()`", "type": "method", "name": "now", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [] } ], "desc": "

Returns the current high resolution millisecond timestamp, where 0 represents\nthe start of the current node process.

" }, { "textRaw": "`performance.timerify(fn[, options])`", "type": "method", "name": "timerify", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37475", "description": "Added the histogram option." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Re-implemented to use pure-JavaScript and the ability to time async functions." } ] }, "signatures": [ { "params": [ { "textRaw": "`fn` {Function}", "name": "fn", "type": "Function" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`histogram` {RecordableHistogram} A histogram object created using `perf_hooks.createHistogram()` that will record runtime durations in nanoseconds.", "name": "histogram", "type": "RecordableHistogram", "desc": "A histogram object created using `perf_hooks.createHistogram()` that will record runtime durations in nanoseconds." } ] } ] } ], "desc": "

This property is an extension by Node.js. It is not available in Web browsers.

\n

Wraps a function within a new function that measures the running time of the\nwrapped function. A PerformanceObserver must be subscribed to the 'function'\nevent type in order for the timing details to be accessed.

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nfunction someFunction() {\n  console.log('hello world');\n}\n\nconst wrapped = performance.timerify(someFunction);\n\nconst obs = new PerformanceObserver((list) => {\n  console.log(list.getEntries()[0].duration);\n  obs.disconnect();\n});\nobs.observe({ entryTypes: ['function'] });\n\n// A performance timeline entry will be created\nwrapped();\n
\n

If the wrapped function returns a promise, a finally handler will be attached\nto the promise and the duration will be reported once the finally handler is\ninvoked.

" }, { "textRaw": "`performance.toJSON()`", "type": "method", "name": "toJSON", "meta": { "added": [ "v16.1.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

An object which is JSON representation of the performance object. It\nis similar to window.performance.toJSON in browsers.

" } ], "properties": [ { "textRaw": "`nodeTiming` {PerformanceNodeTiming}", "type": "PerformanceNodeTiming", "name": "nodeTiming", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

This property is an extension by Node.js. It is not available in Web browsers.

\n

An instance of the PerformanceNodeTiming class that provides performance\nmetrics for specific Node.js operational milestones.

" }, { "textRaw": "`timeOrigin` {number}", "type": "number", "name": "timeOrigin", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The timeOrigin specifies the high resolution millisecond timestamp at\nwhich the current node process began, measured in Unix time.

" } ] } ], "classes": [ { "textRaw": "Class: `PerformanceEntry`", "type": "class", "name": "PerformanceEntry", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "properties": [ { "textRaw": "`detail` {any}", "type": "any", "name": "detail", "meta": { "added": [ "v16.0.0" ], "changes": [] }, "desc": "

Additional detail specific to the entryType.

" }, { "textRaw": "`duration` {number}", "type": "number", "name": "duration", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The total number of milliseconds elapsed for this entry. This value will not\nbe meaningful for all Performance Entry types.

" }, { "textRaw": "`entryType` {string}", "type": "string", "name": "entryType", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The type of the performance entry. It may be one of:

\n" }, { "textRaw": "`flags` {number}", "type": "number", "name": "flags", "meta": { "added": [ "v13.9.0", "v12.17.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Runtime deprecated. Now moved to the detail property when entryType is 'gc'." } ] }, "desc": "

This property is an extension by Node.js. It is not available in Web browsers.

\n

When performanceEntry.entryType is equal to 'gc', the performance.flags\nproperty contains additional information about garbage collection operation.\nThe value may be one of:

\n" }, { "textRaw": "`name` {string}", "type": "string", "name": "name", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The name of the performance entry.

" }, { "textRaw": "`kind` {number}", "type": "number", "name": "kind", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Runtime deprecated. Now moved to the detail property when entryType is 'gc'." } ] }, "desc": "

This property is an extension by Node.js. It is not available in Web browsers.

\n

When performanceEntry.entryType is equal to 'gc', the performance.kind\nproperty identifies the type of garbage collection operation that occurred.\nThe value may be one of:

\n" }, { "textRaw": "`startTime` {number}", "type": "number", "name": "startTime", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp marking the starting time of the\nPerformance Entry.

" } ], "modules": [ { "textRaw": "Garbage Collection ('gc') Details", "name": "garbage_collection_('gc')_details", "desc": "

When performanceEntry.type is equal to 'gc', the performanceEntry.detail\nproperty will be an <Object> with two properties:

\n", "type": "module", "displayName": "Garbage Collection ('gc') Details" }, { "textRaw": "HTTP/2 ('http2') Details", "name": "http/2_('http2')_details", "desc": "

When performanceEntry.type is equal to 'http2', the\nperformanceEntry.detail property will be an <Object> containing\nadditional performance information.

\n

If performanceEntry.name is equal to Http2Stream, the detail\nwill contain the following properties:

\n\n

If performanceEntry.name is equal to Http2Session, the detail will\ncontain the following properties:

\n", "type": "module", "displayName": "HTTP/2 ('http2') Details" }, { "textRaw": "Timerify ('function') Details", "name": "timerify_('function')_details", "desc": "

When performanceEntry.type is equal to 'function', the\nperformanceEntry.detail property will be an <Array> listing\nthe input arguments to the timed function.

", "type": "module", "displayName": "Timerify ('function') Details" } ] }, { "textRaw": "Class: `PerformanceNodeTiming`", "type": "class", "name": "PerformanceNodeTiming", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "\n

This property is an extension by Node.js. It is not available in Web browsers.

\n

Provides timing details for Node.js itself. The constructor of this class\nis not exposed to users.

", "properties": [ { "textRaw": "`bootstrapComplete` {number}", "type": "number", "name": "bootstrapComplete", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the Node.js process\ncompleted bootstrapping. If bootstrapping has not yet finished, the property\nhas the value of -1.

" }, { "textRaw": "`environment` {number}", "type": "number", "name": "environment", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the Node.js environment was\ninitialized.

" }, { "textRaw": "`idleTime` {number}", "type": "number", "name": "idleTime", "meta": { "added": [ "v14.10.0", "v12.19.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp of the amount of time the event loop\nhas been idle within the event loop's event provider (e.g. epoll_wait). This\ndoes not take CPU usage into consideration. If the event loop has not yet\nstarted (e.g., in the first tick of the main script), the property has the\nvalue of 0.

" }, { "textRaw": "`loopExit` {number}", "type": "number", "name": "loopExit", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the Node.js event loop\nexited. If the event loop has not yet exited, the property has the value of -1.\nIt can only have a value of not -1 in a handler of the 'exit' event.

" }, { "textRaw": "`loopStart` {number}", "type": "number", "name": "loopStart", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the Node.js event loop\nstarted. If the event loop has not yet started (e.g., in the first tick of the\nmain script), the property has the value of -1.

" }, { "textRaw": "`nodeStart` {number}", "type": "number", "name": "nodeStart", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the Node.js process was\ninitialized.

" }, { "textRaw": "`v8Start` {number}", "type": "number", "name": "v8Start", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The high resolution millisecond timestamp at which the V8 platform was\ninitialized.

" } ] }, { "textRaw": "Class: `perf_hooks.PerformanceObserver`", "type": "class", "name": "perf_hooks.PerformanceObserver", "methods": [ { "textRaw": "`performanceObserver.disconnect()`", "type": "method", "name": "disconnect", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

Disconnects the PerformanceObserver instance from all notifications.

" }, { "textRaw": "`performanceObserver.observe(options)`", "type": "method", "name": "observe", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v16.7.0", "pr-url": "https://github.com/nodejs/node/pull/39297", "description": "Updated to conform to Performance Timeline Level 2. The buffered option has been added back." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37136", "description": "Updated to conform to User Timing Level 3. The buffered option has been removed." } ] }, "signatures": [ { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`type` {string} A single {PerformanceEntry} type. Must not be given if `entryTypes` is already specified.", "name": "type", "type": "string", "desc": "A single {PerformanceEntry} type. Must not be given if `entryTypes` is already specified." }, { "textRaw": "`entryTypes` {string\\[]} An array of strings identifying the types of {PerformanceEntry} instances the observer is interested in. If not provided an error will be thrown.", "name": "entryTypes", "type": "string\\[]", "desc": "An array of strings identifying the types of {PerformanceEntry} instances the observer is interested in. If not provided an error will be thrown." }, { "textRaw": "`buffered` {boolean} If true, the observer callback is called with a list global `PerformanceEntry` buffered entries. If false, only `PerformanceEntry`s created after the time point are sent to the observer callback. **Default:** `false`.", "name": "buffered", "type": "boolean", "default": "`false`", "desc": "If true, the observer callback is called with a list global `PerformanceEntry` buffered entries. If false, only `PerformanceEntry`s created after the time point are sent to the observer callback." } ] } ] } ], "desc": "

Subscribes the <PerformanceObserver> instance to notifications of new\n<PerformanceEntry> instances identified either by options.entryTypes\nor options.type:

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((list, observer) => {\n  // Called once asynchronously. `list` contains three items.\n});\nobs.observe({ type: 'mark' });\n\nfor (let n = 0; n < 3; n++)\n  performance.mark(`test${n}`);\n
" } ], "signatures": [ { "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`list` {PerformanceObserverEntryList}", "name": "list", "type": "PerformanceObserverEntryList" }, { "textRaw": "`observer` {PerformanceObserver}", "name": "observer", "type": "PerformanceObserver" } ] } ], "desc": "

PerformanceObserver objects provide notifications when new\nPerformanceEntry instances have been added to the Performance Timeline.

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((list, observer) => {\n  console.log(list.getEntries());\n  observer.disconnect();\n});\nobs.observe({ entryTypes: ['mark'], buffered: true });\n\nperformance.mark('test');\n
\n

Because PerformanceObserver instances introduce their own additional\nperformance overhead, instances should not be left subscribed to notifications\nindefinitely. Users should disconnect observers as soon as they are no\nlonger needed.

\n

The callback is invoked when a PerformanceObserver is\nnotified about new PerformanceEntry instances. The callback receives a\nPerformanceObserverEntryList instance and a reference to the\nPerformanceObserver.

" } ] }, { "textRaw": "Class: `PerformanceObserverEntryList`", "type": "class", "name": "PerformanceObserverEntryList", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The PerformanceObserverEntryList class is used to provide access to the\nPerformanceEntry instances passed to a PerformanceObserver.\nThe constructor of this class is not exposed to users.

", "methods": [ { "textRaw": "`performanceObserverEntryList.getEntries()`", "type": "method", "name": "getEntries", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {PerformanceEntry\\[]}", "name": "return", "type": "PerformanceEntry\\[]" }, "params": [] } ], "desc": "

Returns a list of PerformanceEntry objects in chronological order\nwith respect to performanceEntry.startTime.

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((perfObserverList, observer) => {\n  console.log(perfObserverList.getEntries());\n  /**\n   * [\n   *   PerformanceEntry {\n   *     name: 'test',\n   *     entryType: 'mark',\n   *     startTime: 81.465639,\n   *     duration: 0\n   *   },\n   *   PerformanceEntry {\n   *     name: 'meow',\n   *     entryType: 'mark',\n   *     startTime: 81.860064,\n   *     duration: 0\n   *   }\n   * ]\n   */\n  observer.disconnect();\n});\nobs.observe({ type: 'mark' });\n\nperformance.mark('test');\nperformance.mark('meow');\n
" }, { "textRaw": "`performanceObserverEntryList.getEntriesByName(name[, type])`", "type": "method", "name": "getEntriesByName", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {PerformanceEntry\\[]}", "name": "return", "type": "PerformanceEntry\\[]" }, "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string" }, { "textRaw": "`type` {string}", "name": "type", "type": "string" } ] } ], "desc": "

Returns a list of PerformanceEntry objects in chronological order\nwith respect to performanceEntry.startTime whose performanceEntry.name is\nequal to name, and optionally, whose performanceEntry.entryType is equal to\ntype.

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((perfObserverList, observer) => {\n  console.log(perfObserverList.getEntriesByName('meow'));\n  /**\n   * [\n   *   PerformanceEntry {\n   *     name: 'meow',\n   *     entryType: 'mark',\n   *     startTime: 98.545991,\n   *     duration: 0\n   *   }\n   * ]\n   */\n  console.log(perfObserverList.getEntriesByName('nope')); // []\n\n  console.log(perfObserverList.getEntriesByName('test', 'mark'));\n  /**\n   * [\n   *   PerformanceEntry {\n   *     name: 'test',\n   *     entryType: 'mark',\n   *     startTime: 63.518931,\n   *     duration: 0\n   *   }\n   * ]\n   */\n  console.log(perfObserverList.getEntriesByName('test', 'measure')); // []\n  observer.disconnect();\n});\nobs.observe({ entryTypes: ['mark', 'measure'] });\n\nperformance.mark('test');\nperformance.mark('meow');\n
" }, { "textRaw": "`performanceObserverEntryList.getEntriesByType(type)`", "type": "method", "name": "getEntriesByType", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {PerformanceEntry\\[]}", "name": "return", "type": "PerformanceEntry\\[]" }, "params": [ { "textRaw": "`type` {string}", "name": "type", "type": "string" } ] } ], "desc": "

Returns a list of PerformanceEntry objects in chronological order\nwith respect to performanceEntry.startTime whose performanceEntry.entryType\nis equal to type.

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((perfObserverList, observer) => {\n  console.log(perfObserverList.getEntriesByType('mark'));\n  /**\n   * [\n   *   PerformanceEntry {\n   *     name: 'test',\n   *     entryType: 'mark',\n   *     startTime: 55.897834,\n   *     duration: 0\n   *   },\n   *   PerformanceEntry {\n   *     name: 'meow',\n   *     entryType: 'mark',\n   *     startTime: 56.350146,\n   *     duration: 0\n   *   }\n   * ]\n   */\n  observer.disconnect();\n});\nobs.observe({ type: 'mark' });\n\nperformance.mark('test');\nperformance.mark('meow');\n
" } ] }, { "textRaw": "Class: `Histogram`", "type": "class", "name": "Histogram", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "properties": [ { "textRaw": "`count` {number}", "type": "number", "name": "count", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

The number of samples recorded by the histogram.

" }, { "textRaw": "`countBigInt` {bigint}", "type": "bigint", "name": "countBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

The number of samples recorded by the histogram.

" }, { "textRaw": "`exceeds` {number}", "type": "number", "name": "exceeds", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

The number of times the event loop delay exceeded the maximum 1 hour event\nloop delay threshold.

" }, { "textRaw": "`exceedsBigInt` {bigint}", "type": "bigint", "name": "exceedsBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

The number of times the event loop delay exceeded the maximum 1 hour event\nloop delay threshold.

" }, { "textRaw": "`max` {number}", "type": "number", "name": "max", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

The maximum recorded event loop delay.

" }, { "textRaw": "`maxBigInt` {bigint}", "type": "bigint", "name": "maxBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

The maximum recorded event loop delay.

" }, { "textRaw": "`mean` {number}", "type": "number", "name": "mean", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

The mean of the recorded event loop delays.

" }, { "textRaw": "`min` {number}", "type": "number", "name": "min", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

The minimum recorded event loop delay.

" }, { "textRaw": "`minBigInt` {bigint}", "type": "bigint", "name": "minBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

The minimum recorded event loop delay.

" }, { "textRaw": "`percentiles` {Map}", "type": "Map", "name": "percentiles", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

Returns a Map object detailing the accumulated percentile distribution.

" }, { "textRaw": "`percentilesBigInt` {Map}", "type": "Map", "name": "percentilesBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "desc": "

Returns a Map object detailing the accumulated percentile distribution.

" }, { "textRaw": "`stddev` {number}", "type": "number", "name": "stddev", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "desc": "

The standard deviation of the recorded event loop delays.

" } ], "methods": [ { "textRaw": "`histogram.percentile(percentile)`", "type": "method", "name": "percentile", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`percentile` {number} A percentile value in the range (0, 100].", "name": "percentile", "type": "number", "desc": "A percentile value in the range (0, 100]." } ] } ], "desc": "

Returns the value at the given percentile.

" }, { "textRaw": "`histogram.percentileBigInt(percentile)`", "type": "method", "name": "percentileBigInt", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {bigint}", "name": "return", "type": "bigint" }, "params": [ { "textRaw": "`percentile` {number} A percentile value in the range (0, 100).", "name": "percentile", "type": "number", "desc": "A percentile value in the range (0, 100)." } ] } ], "desc": "

Returns the value at the given percentile.

" }, { "textRaw": "`histogram.reset()`", "type": "method", "name": "reset", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

Resets the collected histogram data.

" } ] }, { "textRaw": "Class: `IntervalHistogram extends Histogram`", "type": "class", "name": "IntervalHistogram", "desc": "

A Histogram that is periodically updated on a given interval.

", "methods": [ { "textRaw": "`histogram.disable()`", "type": "method", "name": "disable", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Disables the update interval timer. Returns true if the timer was\nstopped, false if it was already stopped.

" }, { "textRaw": "`histogram.enable()`", "type": "method", "name": "enable", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Enables the update interval timer. Returns true if the timer was\nstarted, false if it was already started.

" } ], "modules": [ { "textRaw": "Cloning an `IntervalHistogram`", "name": "cloning_an_`intervalhistogram`", "desc": "

<IntervalHistogram> instances can be cloned via <MessagePort>. On the receiving\nend, the histogram is cloned as a plain <Histogram> object that does not\nimplement the enable() and disable() methods.

", "type": "module", "displayName": "Cloning an `IntervalHistogram`" } ] }, { "textRaw": "Class: `RecordableHistogram extends Histogram`", "type": "class", "name": "RecordableHistogram", "meta": { "added": [ "v15.9.0", "v14.18.0" ], "changes": [] }, "methods": [ { "textRaw": "`histogram.add(other)`", "type": "method", "name": "add", "meta": { "added": [ "v17.4.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`other` {RecordableHistogram}", "name": "other", "type": "RecordableHistogram" } ] } ], "desc": "

Adds the values from other to this histogram.

" }, { "textRaw": "`histogram.record(val)`", "type": "method", "name": "record", "meta": { "added": [ "v15.9.0", "v14.18.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`val` {number|bigint} The amount to record in the histogram.", "name": "val", "type": "number|bigint", "desc": "The amount to record in the histogram." } ] } ] }, { "textRaw": "`histogram.recordDelta()`", "type": "method", "name": "recordDelta", "meta": { "added": [ "v15.9.0", "v14.18.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

Calculates the amount of time (in nanoseconds) that has passed since the\nprevious call to recordDelta() and records that amount in the histogram.

\n

Examples

" } ], "modules": [ { "textRaw": "Measuring the duration of async operations", "name": "measuring_the_duration_of_async_operations", "desc": "

The following example uses the Async Hooks and Performance APIs to measure\nthe actual duration of a Timeout operation (including the amount of time it took\nto execute the callback).

\n
'use strict';\nconst async_hooks = require('async_hooks');\nconst {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst set = new Set();\nconst hook = async_hooks.createHook({\n  init(id, type) {\n    if (type === 'Timeout') {\n      performance.mark(`Timeout-${id}-Init`);\n      set.add(id);\n    }\n  },\n  destroy(id) {\n    if (set.has(id)) {\n      set.delete(id);\n      performance.mark(`Timeout-${id}-Destroy`);\n      performance.measure(`Timeout-${id}`,\n                          `Timeout-${id}-Init`,\n                          `Timeout-${id}-Destroy`);\n    }\n  }\n});\nhook.enable();\n\nconst obs = new PerformanceObserver((list, observer) => {\n  console.log(list.getEntries()[0]);\n  performance.clearMarks();\n  observer.disconnect();\n});\nobs.observe({ entryTypes: ['measure'], buffered: true });\n\nsetTimeout(() => {}, 1000);\n
", "type": "module", "displayName": "Measuring the duration of async operations" }, { "textRaw": "Measuring how long it takes to load dependencies", "name": "measuring_how_long_it_takes_to_load_dependencies", "desc": "

The following example measures the duration of require() operations to load\ndependencies:

\n\n
'use strict';\nconst {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\nconst mod = require('module');\n\n// Monkey patch the require function\nmod.Module.prototype.require =\n  performance.timerify(mod.Module.prototype.require);\nrequire = performance.timerify(require);\n\n// Activate the observer\nconst obs = new PerformanceObserver((list) => {\n  const entries = list.getEntries();\n  entries.forEach((entry) => {\n    console.log(`require('${entry[0]}')`, entry.duration);\n  });\n  obs.disconnect();\n});\nobs.observe({ entryTypes: ['function'], buffered: true });\n\nrequire('some-module');\n
", "type": "module", "displayName": "Measuring how long it takes to load dependencies" } ] } ], "methods": [ { "textRaw": "`perf_hooks.createHistogram([options])`", "type": "method", "name": "createHistogram", "meta": { "added": [ "v15.9.0", "v14.18.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns {RecordableHistogram}", "name": "return", "type": "RecordableHistogram" }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`lowest` {number|bigint} The lowest discernible value. Must be an integer value greater than 0. **Default:** `1`.", "name": "lowest", "type": "number|bigint", "default": "`1`", "desc": "The lowest discernible value. Must be an integer value greater than 0." }, { "textRaw": "`highest` {number|bigint} The highest recordable value. Must be an integer value that is equal to or greater than two times `min`. **Default:** `Number.MAX_SAFE_INTEGER`.", "name": "highest", "type": "number|bigint", "default": "`Number.MAX_SAFE_INTEGER`", "desc": "The highest recordable value. Must be an integer value that is equal to or greater than two times `min`." }, { "textRaw": "`figures` {number} The number of accuracy digits. Must be a number between `1` and `5`. **Default:** `3`.", "name": "figures", "type": "number", "default": "`3`", "desc": "The number of accuracy digits. Must be a number between `1` and `5`." } ] } ] } ], "desc": "

Returns a <RecordableHistogram>.

" }, { "textRaw": "`perf_hooks.monitorEventLoopDelay([options])`", "type": "method", "name": "monitorEventLoopDelay", "meta": { "added": [ "v11.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {IntervalHistogram}", "name": "return", "type": "IntervalHistogram" }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`resolution` {number} The sampling rate in milliseconds. Must be greater than zero. **Default:** `10`.", "name": "resolution", "type": "number", "default": "`10`", "desc": "The sampling rate in milliseconds. Must be greater than zero." } ] } ] } ], "desc": "

This property is an extension by Node.js. It is not available in Web browsers.

\n

Creates an IntervalHistogram object that samples and reports the event loop\ndelay over time. The delays will be reported in nanoseconds.

\n

Using a timer to detect approximate event loop delay works because the\nexecution of timers is tied specifically to the lifecycle of the libuv\nevent loop. That is, a delay in the loop will cause a delay in the execution\nof the timer, and those delays are specifically what this API is intended to\ndetect.

\n
const { monitorEventLoopDelay } = require('perf_hooks');\nconst h = monitorEventLoopDelay({ resolution: 20 });\nh.enable();\n// Do something.\nh.disable();\nconsole.log(h.min);\nconsole.log(h.max);\nconsole.log(h.mean);\nconsole.log(h.stddev);\nconsole.log(h.percentiles);\nconsole.log(h.percentile(50));\nconsole.log(h.percentile(99));\n
" } ], "type": "module", "displayName": "Performance measurement APIs" } ] }