1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.21.2"> 7 <title>Performance measurement APIs | Node.js v14.21.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/perf_hooks.html"> 12 </head> 13 <body class="alt apidoc" id="api-section-perf_hooks"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22 <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23 <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24 </ul> 25 <hr class="line"> 26 <ul> 27 <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28 <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29 <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30 <li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31 <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32 <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33 <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34 <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35 <li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36 <li><a href="console.html" class="nav-console">Console</a></li> 37 <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38 <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39 <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40 <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41 <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42 <li><a href="dns.html" class="nav-dns">DNS</a></li> 43 <li><a href="domain.html" class="nav-domain">Domain</a></li> 44 <li><a href="errors.html" class="nav-errors">Errors</a></li> 45 <li><a href="events.html" class="nav-events">Events</a></li> 46 <li><a href="fs.html" class="nav-fs">File system</a></li> 47 <li><a href="globals.html" class="nav-globals">Globals</a></li> 48 <li><a href="http.html" class="nav-http">HTTP</a></li> 49 <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50 <li><a href="https.html" class="nav-https">HTTPS</a></li> 51 <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52 <li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53 <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54 <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55 <li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56 <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57 <li><a href="net.html" class="nav-net">Net</a></li> 58 <li><a href="os.html" class="nav-os">OS</a></li> 59 <li><a href="path.html" class="nav-path">Path</a></li> 60 <li><a href="perf_hooks.html" class="nav-perf_hooks active">Performance hooks</a></li> 61 <li><a href="policy.html" class="nav-policy">Policies</a></li> 62 <li><a href="process.html" class="nav-process">Process</a></li> 63 <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64 <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65 <li><a href="readline.html" class="nav-readline">Readline</a></li> 66 <li><a href="repl.html" class="nav-repl">REPL</a></li> 67 <li><a href="report.html" class="nav-report">Report</a></li> 68 <li><a href="stream.html" class="nav-stream">Stream</a></li> 69 <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70 <li><a href="timers.html" class="nav-timers">Timers</a></li> 71 <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72 <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73 <li><a href="tty.html" class="nav-tty">TTY</a></li> 74 <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75 <li><a href="url.html" class="nav-url">URL</a></li> 76 <li><a href="util.html" class="nav-util">Utilities</a></li> 77 <li><a href="v8.html" class="nav-v8">V8</a></li> 78 <li><a href="vm.html" class="nav-vm">VM</a></li> 79 <li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80 <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81 <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82 </ul> 83 <hr class="line"> 84 <ul> 85 <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86 </ul> 87 </div> 88 89 <div id="column1" data-id="perf_hooks" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.21.2 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="perf_hooks.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/perf_hooks.html">19.x</a></li> 120 <li><a href="https://nodejs.org/docs/latest-v18.x/api/perf_hooks.html">18.x <b>LTS</b></a></li> 121 <li><a href="https://nodejs.org/docs/latest-v17.x/api/perf_hooks.html">17.x</a></li> 122 <li><a href="https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html">16.x <b>LTS</b></a></li> 123 <li><a href="https://nodejs.org/docs/latest-v15.x/api/perf_hooks.html">15.x</a></li> 124 <li><a href="https://nodejs.org/docs/latest-v14.x/api/perf_hooks.html">14.x <b>LTS</b></a></li> 125 <li><a href="https://nodejs.org/docs/latest-v13.x/api/perf_hooks.html">13.x</a></li> 126 <li><a href="https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html">12.x</a></li> 127 <li><a href="https://nodejs.org/docs/latest-v11.x/api/perf_hooks.html">11.x</a></li> 128 <li><a href="https://nodejs.org/docs/latest-v10.x/api/perf_hooks.html">10.x</a></li> 129 <li><a href="https://nodejs.org/docs/latest-v9.x/api/perf_hooks.html">9.x</a></li> 130 <li><a href="https://nodejs.org/docs/latest-v8.x/api/perf_hooks.html">8.x</a></li></ol> 131 </li> 132 133 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/perf_hooks.md">Edit on GitHub</a></li> 134 </ul> 135 </div> 136 <hr> 137 </header> 138 139 <details id="toc" open><summary>Table of contents</summary><ul> 140 <li><span class="stability_2"><a href="#perf_hooks_performance_measurement_apis">Performance measurement APIs</a></span> 141 <ul> 142 <li><a href="#perf_hooks_perf_hooks_performance"><code>perf_hooks.performance</code></a> 143 <ul> 144 <li><a href="#perf_hooks_performance_clearmarks_name"><code>performance.clearMarks([name])</code></a></li> 145 <li><a href="#perf_hooks_performance_eventlooputilization_utilization1_utilization2"><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code></a></li> 146 <li><a href="#perf_hooks_performance_mark_name"><code>performance.mark([name])</code></a></li> 147 <li><a href="#perf_hooks_performance_measure_name_startmark_endmark"><code>performance.measure(name[, startMark[, endMark]])</code></a></li> 148 <li><a href="#perf_hooks_performance_nodetiming"><code>performance.nodeTiming</code></a></li> 149 <li><a href="#perf_hooks_performance_now"><code>performance.now()</code></a></li> 150 <li><a href="#perf_hooks_performance_timeorigin"><code>performance.timeOrigin</code></a></li> 151 <li><a href="#perf_hooks_performance_timerify_fn"><code>performance.timerify(fn)</code></a></li> 152 </ul> 153 </li> 154 <li><a href="#perf_hooks_class_performanceentry">Class: <code>PerformanceEntry</code></a> 155 <ul> 156 <li><a href="#perf_hooks_performanceentry_duration"><code>performanceEntry.duration</code></a></li> 157 <li><a href="#perf_hooks_performanceentry_entrytype"><code>performanceEntry.entryType</code></a></li> 158 <li><a href="#perf_hooks_performanceentry_flags"><code>performanceEntry.flags</code></a></li> 159 <li><a href="#perf_hooks_performanceentry_name"><code>performanceEntry.name</code></a></li> 160 <li><a href="#perf_hooks_performanceentry_kind"><code>performanceEntry.kind</code></a></li> 161 <li><a href="#perf_hooks_performanceentry_starttime"><code>performanceEntry.startTime</code></a></li> 162 </ul> 163 </li> 164 <li><a href="#perf_hooks_class_performancenodetiming">Class: <code>PerformanceNodeTiming</code></a> 165 <ul> 166 <li><a href="#perf_hooks_performancenodetiming_bootstrapcomplete"><code>performanceNodeTiming.bootstrapComplete</code></a></li> 167 <li><a href="#perf_hooks_performancenodetiming_environment"><code>performanceNodeTiming.environment</code></a></li> 168 <li><a href="#perf_hooks_performancenodetiming_idletime"><code>performanceNodeTiming.idleTime</code></a></li> 169 <li><a href="#perf_hooks_performancenodetiming_loopexit"><code>performanceNodeTiming.loopExit</code></a></li> 170 <li><a href="#perf_hooks_performancenodetiming_loopstart"><code>performanceNodeTiming.loopStart</code></a></li> 171 <li><a href="#perf_hooks_performancenodetiming_nodestart"><code>performanceNodeTiming.nodeStart</code></a></li> 172 <li><a href="#perf_hooks_performancenodetiming_v8start"><code>performanceNodeTiming.v8Start</code></a></li> 173 </ul> 174 </li> 175 <li><a href="#perf_hooks_class_perf_hooks_performanceobserver">Class: <code>perf_hooks.PerformanceObserver</code></a> 176 <ul> 177 <li><a href="#perf_hooks_new_performanceobserver_callback"><code>new PerformanceObserver(callback)</code></a></li> 178 <li><a href="#perf_hooks_performanceobserver_disconnect"><code>performanceObserver.disconnect()</code></a></li> 179 <li><a href="#perf_hooks_performanceobserver_observe_options"><code>performanceObserver.observe(options)</code></a></li> 180 </ul> 181 </li> 182 <li><a href="#perf_hooks_class_performanceobserverentrylist">Class: <code>PerformanceObserverEntryList</code></a> 183 <ul> 184 <li><a href="#perf_hooks_performanceobserverentrylist_getentries"><code>performanceObserverEntryList.getEntries()</code></a></li> 185 <li><a href="#perf_hooks_performanceobserverentrylist_getentriesbyname_name_type"><code>performanceObserverEntryList.getEntriesByName(name[, type])</code></a></li> 186 <li><a href="#perf_hooks_performanceobserverentrylist_getentriesbytype_type"><code>performanceObserverEntryList.getEntriesByType(type)</code></a></li> 187 </ul> 188 </li> 189 <li><a href="#perf_hooks_perf_hooks_createhistogram_options"><code>perf_hooks.createHistogram([options])</code></a></li> 190 <li><a href="#perf_hooks_perf_hooks_monitoreventloopdelay_options"><code>perf_hooks.monitorEventLoopDelay([options])</code></a></li> 191 <li><a href="#perf_hooks_class_histogram">Class: <code>Histogram</code></a> 192 <ul> 193 <li><a href="#perf_hooks_histogram_exceeds"><code>histogram.exceeds</code></a></li> 194 <li><a href="#perf_hooks_histogram_max"><code>histogram.max</code></a></li> 195 <li><a href="#perf_hooks_histogram_mean"><code>histogram.mean</code></a></li> 196 <li><a href="#perf_hooks_histogram_min"><code>histogram.min</code></a></li> 197 <li><a href="#perf_hooks_histogram_percentile_percentile"><code>histogram.percentile(percentile)</code></a></li> 198 <li><a href="#perf_hooks_histogram_percentiles"><code>histogram.percentiles</code></a></li> 199 <li><a href="#perf_hooks_histogram_reset"><code>histogram.reset()</code></a></li> 200 <li><a href="#perf_hooks_histogram_stddev"><code>histogram.stddev</code></a></li> 201 </ul> 202 </li> 203 <li><a href="#perf_hooks_class_intervalhistogram_extends_histogram">Class: <code>IntervalHistogram extends Histogram</code></a> 204 <ul> 205 <li><a href="#perf_hooks_histogram_disable"><code>histogram.disable()</code></a></li> 206 <li><a href="#perf_hooks_histogram_enable"><code>histogram.enable()</code></a></li> 207 <li><a href="#perf_hooks_cloning_an_intervalhistogram">Cloning an <code>IntervalHistogram</code></a></li> 208 </ul> 209 </li> 210 <li><a href="#perf_hooks_class_recordablehistogram_extends_histogram">Class: <code>RecordableHistogram extends Histogram</code></a> 211 <ul> 212 <li><a href="#perf_hooks_histogram_record_val"><code>histogram.record(val)</code></a></li> 213 <li><a href="#perf_hooks_histogram_recorddelta"><code>histogram.recordDelta()</code></a></li> 214 </ul> 215 </li> 216 <li><a href="#perf_hooks_examples">Examples</a> 217 <ul> 218 <li><a href="#perf_hooks_measuring_the_duration_of_async_operations">Measuring the duration of async operations</a></li> 219 <li><a href="#perf_hooks_measuring_how_long_it_takes_to_load_dependencies">Measuring how long it takes to load dependencies</a></li> 220 </ul> 221 </li> 222 </ul> 223 </li> 224 </ul></details> 225 226 <div id="apicontent"> 227 <h2>Performance measurement APIs<span><a class="mark" href="#perf_hooks_performance_measurement_apis" id="perf_hooks_performance_measurement_apis">#</a></span></h2> 228 229 <p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 230 <p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/perf_hooks.js">lib/perf_hooks.js</a></p> 231 <p>This module provides an implementation of a subset of the W3C 232 <a href="https://w3c.github.io/perf-timing-primer/">Web Performance APIs</a> as well as additional APIs for 233 Node.js-specific performance measurements.</p> 234 <p>Node.js supports the following <a href="https://w3c.github.io/perf-timing-primer/">Web Performance APIs</a>:</p> 235 <ul> 236 <li><a href="https://www.w3.org/TR/hr-time-2">High Resolution Time</a></li> 237 <li><a href="https://w3c.github.io/performance-timeline/">Performance Timeline</a></li> 238 <li><a href="https://www.w3.org/TR/user-timing/">User Timing</a></li> 239 </ul> 240 <pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver</span>, performance } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 241 242 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">items</span>) =></span> { 243 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>].<span class="hljs-property">duration</span>); 244 performance.<span class="hljs-title function_">clearMarks</span>(); 245 }); 246 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'measure'</span>] }); 247 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'Start to Now'</span>); 248 249 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'A'</span>); 250 <span class="hljs-title function_">doSomeLongRunningProcess</span>(<span class="hljs-function">() =></span> { 251 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'A to Now'</span>, <span class="hljs-string">'A'</span>); 252 253 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'B'</span>); 254 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'A to B'</span>, <span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>); 255 });</code></pre> 256 <section><h3><code>perf_hooks.performance</code><span><a class="mark" href="#perf_hooks_perf_hooks_performance" id="perf_hooks_perf_hooks_performance">#</a></span></h3> 257 <div class="api_metadata"> 258 <span>Added in: v8.5.0</span> 259 </div> 260 <p>An object that can be used to collect performance metrics from the current 261 Node.js instance. It is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/performance"><code>window.performance</code></a> in browsers.</p> 262 <h4><code>performance.clearMarks([name])</code><span><a class="mark" href="#perf_hooks_performance_clearmarks_name" id="perf_hooks_performance_clearmarks_name">#</a></span></h4> 263 <div class="api_metadata"> 264 <span>Added in: v8.5.0</span> 265 </div> 266 <ul> 267 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 268 </ul> 269 <p>If <code>name</code> is not provided, removes all <code>PerformanceMark</code> objects from the 270 Performance Timeline. If <code>name</code> is provided, removes only the named mark.</p> 271 <h4><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code><span><a class="mark" href="#perf_hooks_performance_eventlooputilization_utilization1_utilization2" id="perf_hooks_performance_eventlooputilization_utilization1_utilization2">#</a></span></h4> 272 <div class="api_metadata"> 273 <span>Added in: v14.10.0, v12.19.0</span> 274 </div> 275 <ul> 276 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The result of a previous call to 277 <code>eventLoopUtilization()</code>.</li> 278 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The result of a previous call to 279 <code>eventLoopUtilization()</code> prior to <code>utilization1</code>.</li> 280 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 281 <ul> 282 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 283 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 284 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 285 </ul> 286 </li> 287 </ul> 288 <p>The <code>eventLoopUtilization()</code> method returns an object that contains the 289 cumulative duration of time the event loop has been both idle and active as a 290 high resolution milliseconds timer. The <code>utilization</code> value is the calculated 291 Event Loop Utilization (ELU).</p> 292 <p>If bootstrapping has not yet finished on the main thread the properties have 293 the value of <code>0</code>. The ELU is immediately available on <a href="worker_threads.html#worker_threads_worker_threads">Worker threads</a> since 294 bootstrap happens within the event loop.</p> 295 <p>Both <code>utilization1</code> and <code>utilization2</code> are optional parameters.</p> 296 <p>If <code>utilization1</code> is passed, then the delta between the current call's <code>active</code> 297 and <code>idle</code> times, as well as the corresponding <code>utilization</code> value are 298 calculated and returned (similar to <a href="process.html#process_process_hrtime_time"><code>process.hrtime()</code></a>).</p> 299 <p>If <code>utilization1</code> and <code>utilization2</code> are both passed, then the delta is 300 calculated between the two arguments. This is a convenience option because, 301 unlike <a href="process.html#process_process_hrtime_time"><code>process.hrtime()</code></a>, calculating the ELU is more complex than a 302 single subtraction.</p> 303 <p>ELU is similar to CPU utilization, except that it only measures event loop 304 statistics and not CPU usage. It represents the percentage of time the event 305 loop has spent outside the event loop's event provider (e.g. <code>epoll_wait</code>). 306 No other CPU idle time is taken into consideration. The following is an example 307 of how a mostly idle process will have a high ELU.</p> 308 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 309 <span class="hljs-keyword">const</span> { eventLoopUtilization } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>).<span class="hljs-property">performance</span>; 310 <span class="hljs-keyword">const</span> { spawnSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 311 312 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 313 <span class="hljs-keyword">const</span> elu = <span class="hljs-title function_">eventLoopUtilization</span>(); 314 <span class="hljs-title function_">spawnSync</span>(<span class="hljs-string">'sleep'</span>, [<span class="hljs-string">'5'</span>]); 315 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">eventLoopUtilization</span>(elu).<span class="hljs-property">utilization</span>); 316 });</code></pre> 317 <p>Although the CPU is mostly idle while running this script, the value of 318 <code>utilization</code> is <code>1</code>. This is because the call to 319 <a href="child_process.html#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a> blocks the event loop from proceeding.</p> 320 <p>Passing in a user-defined object instead of the result of a previous call to 321 <code>eventLoopUtilization()</code> will lead to undefined behavior. The return values 322 are not guaranteed to reflect any correct state of the event loop.</p> 323 <h4><code>performance.mark([name])</code><span><a class="mark" href="#perf_hooks_performance_mark_name" id="perf_hooks_performance_mark_name">#</a></span></h4> 324 <div class="api_metadata"> 325 <span>Added in: v8.5.0</span> 326 </div> 327 <ul> 328 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 329 </ul> 330 <p>Creates a new <code>PerformanceMark</code> entry in the Performance Timeline. A 331 <code>PerformanceMark</code> is a subclass of <code>PerformanceEntry</code> whose 332 <code>performanceEntry.entryType</code> is always <code>'mark'</code>, and whose 333 <code>performanceEntry.duration</code> is always <code>0</code>. Performance marks are used 334 to mark specific significant moments in the Performance Timeline.</p> 335 <h4><code>performance.measure(name[, startMark[, endMark]])</code><span><a class="mark" href="#perf_hooks_performance_measure_name_startmark_endmark" id="perf_hooks_performance_measure_name_startmark_endmark">#</a></span></h4> 336 <div class="api_metadata"> 337 <details class="changelog"><summary>History</summary> 338 <table> 339 <tbody><tr><th>Version</th><th>Changes</th></tr> 340 <tr><td>v14.0.0</td> 341 <td><p>Make <code>startMark</code> and <code>endMark</code> parameters optional.</p></td></tr> 342 <tr><td>v8.5.0</td> 343 <td><p><span>Added in: v8.5.0</span></p></td></tr> 344 </tbody></table> 345 </details> 346 </div> 347 <ul> 348 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 349 <li><code>startMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Optional.</li> 350 <li><code>endMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Optional.</li> 351 </ul> 352 <p>Creates a new <code>PerformanceMeasure</code> entry in the Performance Timeline. A 353 <code>PerformanceMeasure</code> is a subclass of <code>PerformanceEntry</code> whose 354 <code>performanceEntry.entryType</code> is always <code>'measure'</code>, and whose 355 <code>performanceEntry.duration</code> measures the number of milliseconds elapsed since 356 <code>startMark</code> and <code>endMark</code>.</p> 357 <p>The <code>startMark</code> argument may identify any <em>existing</em> <code>PerformanceMark</code> in the 358 Performance Timeline, or <em>may</em> identify any of the timestamp properties 359 provided by the <code>PerformanceNodeTiming</code> class. If the named <code>startMark</code> does 360 not exist, then <code>startMark</code> is set to <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin"><code>timeOrigin</code></a> by default.</p> 361 <p>The optional <code>endMark</code> argument must identify any <em>existing</em> <code>PerformanceMark</code> 362 in the Performance Timeline or any of the timestamp properties provided by the 363 <code>PerformanceNodeTiming</code> class. <code>endMark</code> will be <code>performance.now()</code> 364 if no parameter is passed, otherwise if the named <code>endMark</code> does not exist, an 365 error will be thrown.</p> 366 <h4><code>performance.nodeTiming</code><span><a class="mark" href="#perf_hooks_performance_nodetiming" id="perf_hooks_performance_nodetiming">#</a></span></h4> 367 <div class="api_metadata"> 368 <span>Added in: v8.5.0</span> 369 </div> 370 <ul> 371 <li><a href="perf_hooks.html#perf_hooks_class_performancenodetiming" class="type"><PerformanceNodeTiming></a></li> 372 </ul> 373 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 374 <p>An instance of the <code>PerformanceNodeTiming</code> class that provides performance 375 metrics for specific Node.js operational milestones.</p> 376 <h4><code>performance.now()</code><span><a class="mark" href="#perf_hooks_performance_now" id="perf_hooks_performance_now">#</a></span></h4> 377 <div class="api_metadata"> 378 <span>Added in: v8.5.0</span> 379 </div> 380 <ul> 381 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 382 </ul> 383 <p>Returns the current high resolution millisecond timestamp, where 0 represents 384 the start of the current <code>node</code> process.</p> 385 <h4><code>performance.timeOrigin</code><span><a class="mark" href="#perf_hooks_performance_timeorigin" id="perf_hooks_performance_timeorigin">#</a></span></h4> 386 <div class="api_metadata"> 387 <span>Added in: v8.5.0</span> 388 </div> 389 <ul> 390 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 391 </ul> 392 <p>The <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin"><code>timeOrigin</code></a> specifies the high resolution millisecond timestamp at 393 which the current <code>node</code> process began, measured in Unix time.</p> 394 <h4><code>performance.timerify(fn)</code><span><a class="mark" href="#perf_hooks_performance_timerify_fn" id="perf_hooks_performance_timerify_fn">#</a></span></h4> 395 <div class="api_metadata"> 396 <span>Added in: v8.5.0</span> 397 </div> 398 <ul> 399 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 400 </ul> 401 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 402 <p>Wraps a function within a new function that measures the running time of the 403 wrapped function. A <code>PerformanceObserver</code> must be subscribed to the <code>'function'</code> 404 event type in order for the timing details to be accessed.</p> 405 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 406 performance, 407 <span class="hljs-title class_">PerformanceObserver</span> 408 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 409 410 <span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunction</span>(<span class="hljs-params"></span>) { 411 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</span>); 412 } 413 414 <span class="hljs-keyword">const</span> wrapped = performance.<span class="hljs-title function_">timerify</span>(someFunction); 415 416 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list</span>) =></span> { 417 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>].<span class="hljs-property">duration</span>); 418 obs.<span class="hljs-title function_">disconnect</span>(); 419 }); 420 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'function'</span>] }); 421 422 <span class="hljs-comment">// A performance timeline entry will be created</span> 423 <span class="hljs-title function_">wrapped</span>();</code></pre> 424 </section><section><h3>Class: <code>PerformanceEntry</code><span><a class="mark" href="#perf_hooks_class_performanceentry" id="perf_hooks_class_performanceentry">#</a></span></h3> 425 <div class="api_metadata"> 426 <span>Added in: v8.5.0</span> 427 </div> 428 <h4><code>performanceEntry.duration</code><span><a class="mark" href="#perf_hooks_performanceentry_duration" id="perf_hooks_performanceentry_duration">#</a></span></h4> 429 <div class="api_metadata"> 430 <span>Added in: v8.5.0</span> 431 </div> 432 <ul> 433 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 434 </ul> 435 <p>The total number of milliseconds elapsed for this entry. This value will not 436 be meaningful for all Performance Entry types.</p> 437 <h4><code>performanceEntry.entryType</code><span><a class="mark" href="#perf_hooks_performanceentry_entrytype" id="perf_hooks_performanceentry_entrytype">#</a></span></h4> 438 <div class="api_metadata"> 439 <span>Added in: v8.5.0</span> 440 </div> 441 <ul> 442 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 443 </ul> 444 <p>The type of the performance entry. It may be one of:</p> 445 <ul> 446 <li><code>'node'</code> (Node.js only)</li> 447 <li><code>'mark'</code> (available on the Web)</li> 448 <li><code>'measure'</code> (available on the Web)</li> 449 <li><code>'gc'</code> (Node.js only)</li> 450 <li><code>'function'</code> (Node.js only)</li> 451 <li><code>'http2'</code> (Node.js only)</li> 452 <li><code>'http'</code> (Node.js only)</li> 453 </ul> 454 <h4><code>performanceEntry.flags</code><span><a class="mark" href="#perf_hooks_performanceentry_flags" id="perf_hooks_performanceentry_flags">#</a></span></h4> 455 <div class="api_metadata"> 456 <span>Added in: v13.9.0, v12.17.0</span> 457 </div> 458 <ul> 459 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 460 </ul> 461 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 462 <p>When <code>performanceEntry.entryType</code> is equal to <code>'gc'</code>, the <code>performance.flags</code> 463 property contains additional information about garbage collection operation. 464 The value may be one of:</p> 465 <ul> 466 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_NO</code></li> 467 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED</code></li> 468 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_FORCED</code></li> 469 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING</code></li> 470 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE</code></li> 471 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY</code></li> 472 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE</code></li> 473 </ul> 474 <h4><code>performanceEntry.name</code><span><a class="mark" href="#perf_hooks_performanceentry_name" id="perf_hooks_performanceentry_name">#</a></span></h4> 475 <div class="api_metadata"> 476 <span>Added in: v8.5.0</span> 477 </div> 478 <ul> 479 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 480 </ul> 481 <p>The name of the performance entry.</p> 482 <h4><code>performanceEntry.kind</code><span><a class="mark" href="#perf_hooks_performanceentry_kind" id="perf_hooks_performanceentry_kind">#</a></span></h4> 483 <div class="api_metadata"> 484 <span>Added in: v8.5.0</span> 485 </div> 486 <ul> 487 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 488 </ul> 489 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 490 <p>When <code>performanceEntry.entryType</code> is equal to <code>'gc'</code>, the <code>performance.kind</code> 491 property identifies the type of garbage collection operation that occurred. 492 The value may be one of:</p> 493 <ul> 494 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR</code></li> 495 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR</code></li> 496 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL</code></li> 497 <li><code>perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB</code></li> 498 </ul> 499 <h4><code>performanceEntry.startTime</code><span><a class="mark" href="#perf_hooks_performanceentry_starttime" id="perf_hooks_performanceentry_starttime">#</a></span></h4> 500 <div class="api_metadata"> 501 <span>Added in: v8.5.0</span> 502 </div> 503 <ul> 504 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 505 </ul> 506 <p>The high resolution millisecond timestamp marking the starting time of the 507 Performance Entry.</p> 508 </section><section><h3>Class: <code>PerformanceNodeTiming</code><span><a class="mark" href="#perf_hooks_class_performancenodetiming" id="perf_hooks_class_performancenodetiming">#</a></span></h3> 509 <div class="api_metadata"> 510 <span>Added in: v8.5.0</span> 511 </div> 512 <ul> 513 <li>Extends: <a href="perf_hooks.html#perf_hooks_class_performanceentry" class="type"><PerformanceEntry></a></li> 514 </ul> 515 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 516 <p>Provides timing details for Node.js itself. The constructor of this class 517 is not exposed to users.</p> 518 <h4><code>performanceNodeTiming.bootstrapComplete</code><span><a class="mark" href="#perf_hooks_performancenodetiming_bootstrapcomplete" id="perf_hooks_performancenodetiming_bootstrapcomplete">#</a></span></h4> 519 <div class="api_metadata"> 520 <span>Added in: v8.5.0</span> 521 </div> 522 <ul> 523 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 524 </ul> 525 <p>The high resolution millisecond timestamp at which the Node.js process 526 completed bootstrapping. If bootstrapping has not yet finished, the property 527 has the value of -1.</p> 528 <h4><code>performanceNodeTiming.environment</code><span><a class="mark" href="#perf_hooks_performancenodetiming_environment" id="perf_hooks_performancenodetiming_environment">#</a></span></h4> 529 <div class="api_metadata"> 530 <span>Added in: v8.5.0</span> 531 </div> 532 <ul> 533 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 534 </ul> 535 <p>The high resolution millisecond timestamp at which the Node.js environment was 536 initialized.</p> 537 <h4><code>performanceNodeTiming.idleTime</code><span><a class="mark" href="#perf_hooks_performancenodetiming_idletime" id="perf_hooks_performancenodetiming_idletime">#</a></span></h4> 538 <div class="api_metadata"> 539 <span>Added in: v14.10.0, v12.19.0</span> 540 </div> 541 <ul> 542 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 543 </ul> 544 <p>The high resolution millisecond timestamp of the amount of time the event loop 545 has been idle within the event loop's event provider (e.g. <code>epoll_wait</code>). This 546 does not take CPU usage into consideration. If the event loop has not yet 547 started (e.g., in the first tick of the main script), the property has the 548 value of 0.</p> 549 <h4><code>performanceNodeTiming.loopExit</code><span><a class="mark" href="#perf_hooks_performancenodetiming_loopexit" id="perf_hooks_performancenodetiming_loopexit">#</a></span></h4> 550 <div class="api_metadata"> 551 <span>Added in: v8.5.0</span> 552 </div> 553 <ul> 554 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 555 </ul> 556 <p>The high resolution millisecond timestamp at which the Node.js event loop 557 exited. If the event loop has not yet exited, the property has the value of -1. 558 It can only have a value of not -1 in a handler of the <a href="process.html#process_event_exit"><code>'exit'</code></a> event.</p> 559 <h4><code>performanceNodeTiming.loopStart</code><span><a class="mark" href="#perf_hooks_performancenodetiming_loopstart" id="perf_hooks_performancenodetiming_loopstart">#</a></span></h4> 560 <div class="api_metadata"> 561 <span>Added in: v8.5.0</span> 562 </div> 563 <ul> 564 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 565 </ul> 566 <p>The high resolution millisecond timestamp at which the Node.js event loop 567 started. If the event loop has not yet started (e.g., in the first tick of the 568 main script), the property has the value of -1.</p> 569 <h4><code>performanceNodeTiming.nodeStart</code><span><a class="mark" href="#perf_hooks_performancenodetiming_nodestart" id="perf_hooks_performancenodetiming_nodestart">#</a></span></h4> 570 <div class="api_metadata"> 571 <span>Added in: v8.5.0</span> 572 </div> 573 <ul> 574 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 575 </ul> 576 <p>The high resolution millisecond timestamp at which the Node.js process was 577 initialized.</p> 578 <h4><code>performanceNodeTiming.v8Start</code><span><a class="mark" href="#perf_hooks_performancenodetiming_v8start" id="perf_hooks_performancenodetiming_v8start">#</a></span></h4> 579 <div class="api_metadata"> 580 <span>Added in: v8.5.0</span> 581 </div> 582 <ul> 583 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 584 </ul> 585 <p>The high resolution millisecond timestamp at which the V8 platform was 586 initialized.</p> 587 </section><section><h3>Class: <code>perf_hooks.PerformanceObserver</code><span><a class="mark" href="#perf_hooks_class_perf_hooks_performanceobserver" id="perf_hooks_class_perf_hooks_performanceobserver">#</a></span></h3> 588 <h4><code>new PerformanceObserver(callback)</code><span><a class="mark" href="#perf_hooks_new_performanceobserver_callback" id="perf_hooks_new_performanceobserver_callback">#</a></span></h4> 589 <div class="api_metadata"> 590 <span>Added in: v8.5.0</span> 591 </div> 592 <ul> 593 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 594 <ul> 595 <li><code>list</code> <a href="perf_hooks.html#perf_hooks_class_performanceobserverentrylist" class="type"><PerformanceObserverEntryList></a></li> 596 <li><code>observer</code> <a href="perf_hooks.html#perf_hooks_class_perf_hooks_performanceobserver" class="type"><PerformanceObserver></a></li> 597 </ul> 598 </li> 599 </ul> 600 <p><code>PerformanceObserver</code> objects provide notifications when new 601 <code>PerformanceEntry</code> instances have been added to the Performance Timeline.</p> 602 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 603 performance, 604 <span class="hljs-title class_">PerformanceObserver</span> 605 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 606 607 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list, observer</span>) =></span> { 608 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class="hljs-title function_">getEntries</span>()); 609 observer.<span class="hljs-title function_">disconnect</span>(); 610 }); 611 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 612 613 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);</code></pre> 614 <p>Because <code>PerformanceObserver</code> instances introduce their own additional 615 performance overhead, instances should not be left subscribed to notifications 616 indefinitely. Users should disconnect observers as soon as they are no 617 longer needed.</p> 618 <p>The <code>callback</code> is invoked when a <code>PerformanceObserver</code> is 619 notified about new <code>PerformanceEntry</code> instances. The callback receives a 620 <code>PerformanceObserverEntryList</code> instance and a reference to the 621 <code>PerformanceObserver</code>.</p> 622 <h4><code>performanceObserver.disconnect()</code><span><a class="mark" href="#perf_hooks_performanceobserver_disconnect" id="perf_hooks_performanceobserver_disconnect">#</a></span></h4> 623 <div class="api_metadata"> 624 <span>Added in: v8.5.0</span> 625 </div> 626 <p>Disconnects the <code>PerformanceObserver</code> instance from all notifications.</p> 627 <h4><code>performanceObserver.observe(options)</code><span><a class="mark" href="#perf_hooks_performanceobserver_observe_options" id="perf_hooks_performanceobserver_observe_options">#</a></span></h4> 628 <div class="api_metadata"> 629 <span>Added in: v8.5.0</span> 630 </div> 631 <ul> 632 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 633 <ul> 634 <li><code>entryTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array of strings identifying the types of 635 <code>PerformanceEntry</code> instances the observer is interested in. If not 636 provided an error will be thrown.</li> 637 <li><code>buffered</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If true, the notification callback will be 638 called using <code>setImmediate()</code> and multiple <code>PerformanceEntry</code> instance 639 notifications will be buffered internally. If <code>false</code>, notifications will 640 be immediate and synchronous. <strong>Default:</strong> <code>false</code>.</li> 641 </ul> 642 </li> 643 </ul> 644 <p>Subscribes the <code>PerformanceObserver</code> instance to notifications of new 645 <code>PerformanceEntry</code> instances identified by <code>options.entryTypes</code>.</p> 646 <p>When <code>options.buffered</code> is <code>false</code>, the <code>callback</code> will be invoked once for 647 every <code>PerformanceEntry</code> instance:</p> 648 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 649 performance, 650 <span class="hljs-title class_">PerformanceObserver</span> 651 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 652 653 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list, observer</span>) =></span> { 654 <span class="hljs-comment">// Called three times synchronously. `list` contains one item.</span> 655 }); 656 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>] }); 657 658 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < <span class="hljs-number">3</span>; n++) 659 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span class="hljs-subst">${n}</span>`</span>);</code></pre> 660 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 661 performance, 662 <span class="hljs-title class_">PerformanceObserver</span> 663 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 664 665 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list, observer</span>) =></span> { 666 <span class="hljs-comment">// Called once. `list` contains three items.</span> 667 }); 668 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 669 670 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < <span class="hljs-number">3</span>; n++) 671 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span class="hljs-subst">${n}</span>`</span>);</code></pre> 672 </section><section><h3>Class: <code>PerformanceObserverEntryList</code><span><a class="mark" href="#perf_hooks_class_performanceobserverentrylist" id="perf_hooks_class_performanceobserverentrylist">#</a></span></h3> 673 <div class="api_metadata"> 674 <span>Added in: v8.5.0</span> 675 </div> 676 <p>The <code>PerformanceObserverEntryList</code> class is used to provide access to the 677 <code>PerformanceEntry</code> instances passed to a <code>PerformanceObserver</code>. 678 The constructor of this class is not exposed to users.</p> 679 <h4><code>performanceObserverEntryList.getEntries()</code><span><a class="mark" href="#perf_hooks_performanceobserverentrylist_getentries" id="perf_hooks_performanceobserverentrylist_getentries">#</a></span></h4> 680 <div class="api_metadata"> 681 <span>Added in: v8.5.0</span> 682 </div> 683 <ul> 684 <li>Returns: <a href="perf_hooks.html#perf_hooks_class_performanceentry" class="type"><PerformanceEntry[]></a></li> 685 </ul> 686 <p>Returns a list of <code>PerformanceEntry</code> objects in chronological order 687 with respect to <code>performanceEntry.startTime</code>.</p> 688 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 689 performance, 690 <span class="hljs-title class_">PerformanceObserver</span> 691 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 692 693 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">perfObserverList, observer</span>) =></span> { 694 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntries</span>()); 695 <span class="hljs-comment">/** 696 * [ 697 * PerformanceEntry { 698 * name: 'test', 699 * entryType: 'mark', 700 * startTime: 81.465639, 701 * duration: 0 702 * }, 703 * PerformanceEntry { 704 * name: 'meow', 705 * entryType: 'mark', 706 * startTime: 81.860064, 707 * duration: 0 708 * } 709 * ] 710 */</span> 711 observer.<span class="hljs-title function_">disconnect</span>(); 712 }); 713 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 714 715 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>); 716 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>);</code></pre> 717 <h4><code>performanceObserverEntryList.getEntriesByName(name[, type])</code><span><a class="mark" href="#perf_hooks_performanceobserverentrylist_getentriesbyname_name_type" id="perf_hooks_performanceobserverentrylist_getentriesbyname_name_type">#</a></span></h4> 718 <div class="api_metadata"> 719 <span>Added in: v8.5.0</span> 720 </div> 721 <ul> 722 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 723 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 724 <li>Returns: <a href="perf_hooks.html#perf_hooks_class_performanceentry" class="type"><PerformanceEntry[]></a></li> 725 </ul> 726 <p>Returns a list of <code>PerformanceEntry</code> objects in chronological order 727 with respect to <code>performanceEntry.startTime</code> whose <code>performanceEntry.name</code> is 728 equal to <code>name</code>, and optionally, whose <code>performanceEntry.entryType</code> is equal to 729 <code>type</code>.</p> 730 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 731 performance, 732 <span class="hljs-title class_">PerformanceObserver</span> 733 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 734 735 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">perfObserverList, observer</span>) =></span> { 736 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntriesByName</span>(<span class="hljs-string">'meow'</span>)); 737 <span class="hljs-comment">/** 738 * [ 739 * PerformanceEntry { 740 * name: 'meow', 741 * entryType: 'mark', 742 * startTime: 98.545991, 743 * duration: 0 744 * } 745 * ] 746 */</span> 747 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntriesByName</span>(<span class="hljs-string">'nope'</span>)); <span class="hljs-comment">// []</span> 748 749 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntriesByName</span>(<span class="hljs-string">'test'</span>, <span class="hljs-string">'mark'</span>)); 750 <span class="hljs-comment">/** 751 * [ 752 * PerformanceEntry { 753 * name: 'test', 754 * entryType: 'mark', 755 * startTime: 63.518931, 756 * duration: 0 757 * } 758 * ] 759 */</span> 760 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntriesByName</span>(<span class="hljs-string">'test'</span>, <span class="hljs-string">'measure'</span>)); <span class="hljs-comment">// []</span> 761 observer.<span class="hljs-title function_">disconnect</span>(); 762 }); 763 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>, <span class="hljs-string">'measure'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 764 765 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>); 766 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>);</code></pre> 767 <h4><code>performanceObserverEntryList.getEntriesByType(type)</code><span><a class="mark" href="#perf_hooks_performanceobserverentrylist_getentriesbytype_type" id="perf_hooks_performanceobserverentrylist_getentriesbytype_type">#</a></span></h4> 768 <div class="api_metadata"> 769 <span>Added in: v8.5.0</span> 770 </div> 771 <ul> 772 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 773 <li>Returns: <a href="perf_hooks.html#perf_hooks_class_performanceentry" class="type"><PerformanceEntry[]></a></li> 774 </ul> 775 <p>Returns a list of <code>PerformanceEntry</code> objects in chronological order 776 with respect to <code>performanceEntry.startTime</code> whose <code>performanceEntry.entryType</code> 777 is equal to <code>type</code>.</p> 778 <pre><code class="language-js"><span class="hljs-keyword">const</span> { 779 performance, 780 <span class="hljs-title class_">PerformanceObserver</span> 781 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 782 783 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">perfObserverList, observer</span>) =></span> { 784 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList.<span class="hljs-title function_">getEntriesByType</span>(<span class="hljs-string">'mark'</span>)); 785 <span class="hljs-comment">/** 786 * [ 787 * PerformanceEntry { 788 * name: 'test', 789 * entryType: 'mark', 790 * startTime: 55.897834, 791 * duration: 0 792 * }, 793 * PerformanceEntry { 794 * name: 'meow', 795 * entryType: 'mark', 796 * startTime: 56.350146, 797 * duration: 0 798 * } 799 * ] 800 */</span> 801 observer.<span class="hljs-title function_">disconnect</span>(); 802 }); 803 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'mark'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 804 805 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>); 806 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>);</code></pre> 807 </section><section><h3><code>perf_hooks.createHistogram([options])</code><span><a class="mark" href="#perf_hooks_perf_hooks_createhistogram_options" id="perf_hooks_perf_hooks_createhistogram_options">#</a></span></h3> 808 <div class="api_metadata"> 809 <span>Added in: v14.18.0</span> 810 </div> 811 <ul> 812 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 813 <ul> 814 <li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> The minimum recordable value. Must be an integer 815 value greater than 0. <strong>Defaults</strong>: <code>1</code>.</li> 816 <li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> The maximum recordable value. Must be an integer 817 value greater than <code>min</code>. <strong>Defaults</strong>: <code>Number.MAX_SAFE_INTEGER</code>.</li> 818 <li><code>figures</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of accuracy digits. Must be a number between 819 <code>1</code> and <code>5</code>. <strong>Defaults</strong>: <code>3</code>.</li> 820 </ul> 821 </li> 822 <li>Returns <a href="perf_hooks.html#perf_hooks_class_recordablehistogram_extends_histogram" class="type"><RecordableHistogram></a></li> 823 </ul> 824 <p>Returns a <a href="perf_hooks.html#perf_hooks_class_recordablehistogram_extends_histogram" class="type"><RecordableHistogram></a>.</p> 825 </section><section><h3><code>perf_hooks.monitorEventLoopDelay([options])</code><span><a class="mark" href="#perf_hooks_perf_hooks_monitoreventloopdelay_options" id="perf_hooks_perf_hooks_monitoreventloopdelay_options">#</a></span></h3> 826 <div class="api_metadata"> 827 <span>Added in: v11.10.0</span> 828 </div> 829 <ul> 830 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 831 <ul> 832 <li><code>resolution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The sampling rate in milliseconds. Must be greater 833 than zero. <strong>Default:</strong> <code>10</code>.</li> 834 </ul> 835 </li> 836 <li>Returns: <a href="perf_hooks.html#perf_hooks_class_intervalhistogram_extends_histogram" class="type"><IntervalHistogram></a></li> 837 </ul> 838 <p><em>This property is an extension by Node.js. It is not available in Web browsers.</em></p> 839 <p>Creates an <code>IntervalHistogram</code> object that samples and reports the event loop 840 delay over time. The delays will be reported in nanoseconds.</p> 841 <p>Using a timer to detect approximate event loop delay works because the 842 execution of timers is tied specifically to the lifecycle of the libuv 843 event loop. That is, a delay in the loop will cause a delay in the execution 844 of the timer, and those delays are specifically what this API is intended to 845 detect.</p> 846 <pre><code class="language-js"><span class="hljs-keyword">const</span> { monitorEventLoopDelay } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 847 <span class="hljs-keyword">const</span> h = <span class="hljs-title function_">monitorEventLoopDelay</span>({ <span class="hljs-attr">resolution</span>: <span class="hljs-number">20</span> }); 848 h.<span class="hljs-title function_">enable</span>(); 849 <span class="hljs-comment">// Do something.</span> 850 h.<span class="hljs-title function_">disable</span>(); 851 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-property">min</span>); 852 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-property">max</span>); 853 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-property">mean</span>); 854 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-property">stddev</span>); 855 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-property">percentiles</span>); 856 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-title function_">percentile</span>(<span class="hljs-number">50</span>)); 857 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="hljs-title function_">percentile</span>(<span class="hljs-number">99</span>));</code></pre> 858 </section><section><h3>Class: <code>Histogram</code><span><a class="mark" href="#perf_hooks_class_histogram" id="perf_hooks_class_histogram">#</a></span></h3> 859 <div class="api_metadata"> 860 <span>Added in: v11.10.0</span> 861 </div> 862 <h4><code>histogram.exceeds</code><span><a class="mark" href="#perf_hooks_histogram_exceeds" id="perf_hooks_histogram_exceeds">#</a></span></h4> 863 <div class="api_metadata"> 864 <span>Added in: v11.10.0</span> 865 </div> 866 <ul> 867 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 868 </ul> 869 <p>The number of times the event loop delay exceeded the maximum 1 hour event 870 loop delay threshold.</p> 871 <h4><code>histogram.max</code><span><a class="mark" href="#perf_hooks_histogram_max" id="perf_hooks_histogram_max">#</a></span></h4> 872 <div class="api_metadata"> 873 <span>Added in: v11.10.0</span> 874 </div> 875 <ul> 876 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 877 </ul> 878 <p>The maximum recorded event loop delay.</p> 879 <h4><code>histogram.mean</code><span><a class="mark" href="#perf_hooks_histogram_mean" id="perf_hooks_histogram_mean">#</a></span></h4> 880 <div class="api_metadata"> 881 <span>Added in: v11.10.0</span> 882 </div> 883 <ul> 884 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 885 </ul> 886 <p>The mean of the recorded event loop delays.</p> 887 <h4><code>histogram.min</code><span><a class="mark" href="#perf_hooks_histogram_min" id="perf_hooks_histogram_min">#</a></span></h4> 888 <div class="api_metadata"> 889 <span>Added in: v11.10.0</span> 890 </div> 891 <ul> 892 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 893 </ul> 894 <p>The minimum recorded event loop delay.</p> 895 <h4><code>histogram.percentile(percentile)</code><span><a class="mark" href="#perf_hooks_histogram_percentile_percentile" id="perf_hooks_histogram_percentile_percentile">#</a></span></h4> 896 <div class="api_metadata"> 897 <span>Added in: v11.10.0</span> 898 </div> 899 <ul> 900 <li><code>percentile</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A percentile value in the range (0, 100].</li> 901 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 902 </ul> 903 <p>Returns the value at the given percentile.</p> 904 <h4><code>histogram.percentiles</code><span><a class="mark" href="#perf_hooks_histogram_percentiles" id="perf_hooks_histogram_percentiles">#</a></span></h4> 905 <div class="api_metadata"> 906 <span>Added in: v11.10.0</span> 907 </div> 908 <ul> 909 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></a></li> 910 </ul> 911 <p>Returns a <code>Map</code> object detailing the accumulated percentile distribution.</p> 912 <h4><code>histogram.reset()</code><span><a class="mark" href="#perf_hooks_histogram_reset" id="perf_hooks_histogram_reset">#</a></span></h4> 913 <div class="api_metadata"> 914 <span>Added in: v11.10.0</span> 915 </div> 916 <p>Resets the collected histogram data.</p> 917 <h4><code>histogram.stddev</code><span><a class="mark" href="#perf_hooks_histogram_stddev" id="perf_hooks_histogram_stddev">#</a></span></h4> 918 <div class="api_metadata"> 919 <span>Added in: v11.10.0</span> 920 </div> 921 <ul> 922 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 923 </ul> 924 <p>The standard deviation of the recorded event loop delays.</p> 925 </section><section><h3>Class: <code>IntervalHistogram extends Histogram</code><span><a class="mark" href="#perf_hooks_class_intervalhistogram_extends_histogram" id="perf_hooks_class_intervalhistogram_extends_histogram">#</a></span></h3> 926 <p>A <code>Histogram</code> that is periodically updated on a given interval.</p> 927 <h4><code>histogram.disable()</code><span><a class="mark" href="#perf_hooks_histogram_disable" id="perf_hooks_histogram_disable">#</a></span></h4> 928 <div class="api_metadata"> 929 <span>Added in: v11.10.0</span> 930 </div> 931 <ul> 932 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 933 </ul> 934 <p>Disables the update interval timer. Returns <code>true</code> if the timer was 935 stopped, <code>false</code> if it was already stopped.</p> 936 <h4><code>histogram.enable()</code><span><a class="mark" href="#perf_hooks_histogram_enable" id="perf_hooks_histogram_enable">#</a></span></h4> 937 <div class="api_metadata"> 938 <span>Added in: v11.10.0</span> 939 </div> 940 <ul> 941 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 942 </ul> 943 <p>Enables the update interval timer. Returns <code>true</code> if the timer was 944 started, <code>false</code> if it was already started.</p> 945 <h4>Cloning an <code>IntervalHistogram</code><span><a class="mark" href="#perf_hooks_cloning_an_intervalhistogram" id="perf_hooks_cloning_an_intervalhistogram">#</a></span></h4> 946 <p><a href="perf_hooks.html#perf_hooks_class_intervalhistogram_extends_histogram" class="type"><IntervalHistogram></a> instances can be cloned via <a href="worker_threads.html#worker_threads_class_messageport" class="type"><MessagePort></a>. On the receiving 947 end, the histogram is cloned as a plain <a href="perf_hooks.html#perf_hooks_class_histogram" class="type"><Histogram></a> object that does not 948 implement the <code>enable()</code> and <code>disable()</code> methods.</p> 949 </section><section><h3>Class: <code>RecordableHistogram extends Histogram</code><span><a class="mark" href="#perf_hooks_class_recordablehistogram_extends_histogram" id="perf_hooks_class_recordablehistogram_extends_histogram">#</a></span></h3> 950 <div class="api_metadata"> 951 <span>Added in: v14.18.0</span> 952 </div> 953 <h4><code>histogram.record(val)</code><span><a class="mark" href="#perf_hooks_histogram_record_val" id="perf_hooks_histogram_record_val">#</a></span></h4> 954 <div class="api_metadata"> 955 <span>Added in: v14.18.0</span> 956 </div> 957 <ul> 958 <li><code>val</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> The amount to record in the histogram.</li> 959 </ul> 960 <h4><code>histogram.recordDelta()</code><span><a class="mark" href="#perf_hooks_histogram_recorddelta" id="perf_hooks_histogram_recorddelta">#</a></span></h4> 961 <div class="api_metadata"> 962 <span>Added in: v14.18.0</span> 963 </div> 964 <p>Calculates the amount of time (in nanoseconds) that has passed since the 965 previous call to <code>recordDelta()</code> and records that amount in the histogram.</p> 966 </section><section><h3>Examples<span><a class="mark" href="#perf_hooks_examples" id="perf_hooks_examples">#</a></span></h3> 967 <h4>Measuring the duration of async operations<span><a class="mark" href="#perf_hooks_measuring_the_duration_of_async_operations" id="perf_hooks_measuring_the_duration_of_async_operations">#</a></span></h4> 968 <p>The following example uses the <a href="async_hooks.html">Async Hooks</a> and Performance APIs to measure 969 the actual duration of a Timeout operation (including the amount of time it took 970 to execute the callback).</p> 971 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 972 <span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async_hooks'</span>); 973 <span class="hljs-keyword">const</span> { 974 performance, 975 <span class="hljs-title class_">PerformanceObserver</span> 976 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 977 978 <span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(); 979 <span class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">createHook</span>({ 980 <span class="hljs-title function_">init</span>(<span class="hljs-params">id, type</span>) { 981 <span class="hljs-keyword">if</span> (type === <span class="hljs-string">'Timeout'</span>) { 982 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Init`</span>); 983 set.<span class="hljs-title function_">add</span>(id); 984 } 985 }, 986 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">id</span>) { 987 <span class="hljs-keyword">if</span> (set.<span class="hljs-title function_">has</span>(id)) { 988 set.<span class="hljs-title function_">delete</span>(id); 989 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Destroy`</span>); 990 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>`</span>, 991 <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Init`</span>, 992 <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Destroy`</span>); 993 } 994 } 995 }); 996 hook.<span class="hljs-title function_">enable</span>(); 997 998 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list, observer</span>) =></span> { 999 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>]); 1000 performance.<span class="hljs-title function_">clearMarks</span>(); 1001 observer.<span class="hljs-title function_">disconnect</span>(); 1002 }); 1003 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'measure'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 1004 1005 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span class="hljs-number">1000</span>);</code></pre> 1006 <h4>Measuring how long it takes to load dependencies<span><a class="mark" href="#perf_hooks_measuring_how_long_it_takes_to_load_dependencies" id="perf_hooks_measuring_how_long_it_takes_to_load_dependencies">#</a></span></h4> 1007 <p>The following example measures the duration of <code>require()</code> operations to load 1008 dependencies:</p> 1009 <!-- eslint-disable no-global-assign --> 1010 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 1011 <span class="hljs-keyword">const</span> { 1012 performance, 1013 <span class="hljs-title class_">PerformanceObserver</span> 1014 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>); 1015 <span class="hljs-keyword">const</span> mod = <span class="hljs-built_in">require</span>(<span class="hljs-string">'module'</span>); 1016 1017 <span class="hljs-comment">// Monkey patch the require function</span> 1018 mod.<span class="hljs-property">Module</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">require</span> = 1019 performance.<span class="hljs-title function_">timerify</span>(mod.<span class="hljs-property">Module</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">require</span>); 1020 <span class="hljs-built_in">require</span> = performance.<span class="hljs-title function_">timerify</span>(<span class="hljs-built_in">require</span>); 1021 1022 <span class="hljs-comment">// Activate the observer</span> 1023 <span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">list</span>) =></span> { 1024 <span class="hljs-keyword">const</span> entries = list.<span class="hljs-title function_">getEntries</span>(); 1025 entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">entry</span>) =></span> { 1026 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`require('<span class="hljs-subst">${entry[<span class="hljs-number">0</span>]}</span>')`</span>, entry.<span class="hljs-property">duration</span>); 1027 }); 1028 obs.<span class="hljs-title function_">disconnect</span>(); 1029 }); 1030 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'function'</span>], <span class="hljs-attr">buffered</span>: <span class="hljs-literal">true</span> }); 1031 1032 <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-module'</span>);</code></pre></section> 1033 <!-- API END --> 1034 </div> 1035 </div> 1036 </div> 1037 <script> 1038 'use strict'; 1039 { 1040 const kCustomPreference = 'customDarkTheme'; 1041 const userSettings = sessionStorage.getItem(kCustomPreference); 1042 const themeToggleButton = document.getElementById('theme-toggle-btn'); 1043 if (userSettings === null && window.matchMedia) { 1044 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 1045 if ('onchange' in mq) { 1046 function mqChangeListener(e) { 1047 document.body.classList.toggle('dark-mode', e.matches); 1048 } 1049 mq.addEventListener('change', mqChangeListener); 1050 if (themeToggleButton) { 1051 themeToggleButton.addEventListener('click', function() { 1052 mq.removeEventListener('change', mqChangeListener); 1053 }, { once: true }); 1054 } 1055 } 1056 if (mq.matches) { 1057 document.body.classList.add('dark-mode'); 1058 } 1059 } else if (userSettings === 'true') { 1060 document.body.classList.add('dark-mode'); 1061 } 1062 if (themeToggleButton) { 1063 themeToggleButton.hidden = false; 1064 themeToggleButton.addEventListener('click', function() { 1065 sessionStorage.setItem( 1066 kCustomPreference, 1067 document.body.classList.toggle('dark-mode') 1068 ); 1069 }); 1070 } 1071 } 1072 </script> 1073 </body> 1074 </html> 1075