{ "type": "module", "source": "doc/api/perf_hooks.md", "modules": [ { "textRaw": "Performance Timing API", "name": "performance_timing_api", "introduced_in": "v8.5.0", "stability": 1, "stabilityText": "Experimental", "desc": "

The Performance Timing API provides an implementation of the\nW3C Performance Timeline specification. The purpose of the API\nis to support collection of high resolution performance metrics.\nThis is the same Performance API as implemented in modern Web browsers.

\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({ entryTypes: ['measure'] });\n\nperformance.mark('A');\ndoSomeLongRunningProcess(() => {\n  performance.mark('B');\n  performance.measure('A to B', 'A', 'B');\n});\n
", "classes": [ { "textRaw": "Class: Performance", "type": "class", "name": "Performance", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "methods": [ { "textRaw": "performance.clearMarks([name])", "type": "method", "name": "clearMarks", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string", "optional": true } ] } ], "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.mark([name])", "type": "method", "name": "mark", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string", "optional": true } ] } ], "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, startMark, endMark)", "type": "method", "name": "measure", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`name` {string}", "name": "name", "type": "string" }, { "textRaw": "`startMark` {string}", "name": "startMark", "type": "string" }, { "textRaw": "`endMark` {string}", "name": "endMark", "type": "string" } ] } ], "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, then startMark is set to timeOrigin by default.

\n

The endMark argument must identify any existing PerformanceMark in the\nPerformance Timeline or any of the timestamp properties provided by the\nPerformanceNodeTiming class. 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)", "type": "method", "name": "timerify", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fn` {Function}", "name": "fn", "type": "Function" } ] } ], "desc": "

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
" } ], "properties": [ { "textRaw": "`nodeTiming` {PerformanceNodeTiming}", "type": "PerformanceNodeTiming", "name": "nodeTiming", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

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.

" } ] }, { "textRaw": "Class: PerformanceEntry", "type": "class", "name": "PerformanceEntry", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "properties": [ { "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": "`name` {string}", "type": "string", "name": "name", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

The name of the performance entry.

" }, { "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.

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

The type of the performance entry. Currently it may be one of: 'node',\n'mark', 'measure', 'gc', 'function', or 'http2'.

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

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": "Class: PerformanceNodeTiming extends PerformanceEntry", "type": "class", "name": "PerformanceNodeTiming", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "desc": "

Provides timing details for Node.js itself.

", "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": "`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: PerformanceObserver", "type": "class", "name": "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": [] }, "signatures": [ { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "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 notification callback will be called using `setImmediate()` and multiple `PerformanceEntry` instance notifications will be buffered internally. If `false`, notifications will be immediate and synchronous. **Default:** `false`.", "name": "buffered", "type": "boolean", "default": "`false`", "desc": "If true, the notification callback will be called using `setImmediate()` and multiple `PerformanceEntry` instance notifications will be buffered internally. If `false`, notifications will be immediate and synchronous." } ] } ] } ], "desc": "

Subscribes the PerformanceObserver instance to notifications of new\nPerformanceEntry instances identified by options.entryTypes.

\n

When options.buffered is false, the callback will be invoked once for\nevery PerformanceEntry instance:

\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((list, observer) => {\n  // called three times synchronously. list contains one item\n});\nobs.observe({ entryTypes: ['mark'] });\n\nfor (let n = 0; n < 3; n++)\n  performance.mark(`test${n}`);\n
\n
const {\n  performance,\n  PerformanceObserver\n} = require('perf_hooks');\n\nconst obs = new PerformanceObserver((list, observer) => {\n  // called once. list contains three items\n});\nobs.observe({ entryTypes: ['mark'], buffered: true });\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.

", "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.

" }, { "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", "optional": true } ] } ], "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.

" }, { "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

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\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" } ] } ], "type": "module", "displayName": "Performance Timing API" } ] }