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="v18.18.2"> 7 <title>V8 | Node.js v18.18.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/v8.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:574px){.with-44-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-v8"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8 active">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="v8" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <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"/> 104 <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"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <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"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><a href="#v8">V8</a> 124<ul> 125<li><a href="#v8cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li> 126<li><a href="#v8getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li> 127<li><a href="#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li> 128<li><a href="#v8getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li> 129<li><a href="#v8getheapstatistics"><code>v8.getHeapStatistics()</code></a></li> 130<li><a href="#v8setflagsfromstringflags"><code>v8.setFlagsFromString(flags)</code></a></li> 131<li><a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a></li> 132<li><a href="#v8takecoverage"><code>v8.takeCoverage()</code></a></li> 133<li><a href="#v8writeheapsnapshotfilename"><code>v8.writeHeapSnapshot([filename])</code></a></li> 134<li><span class="stability_1"><a href="#v8setheapsnapshotnearheaplimitlimit"><code>v8.setHeapSnapshotNearHeapLimit(limit)</code></a></span></li> 135<li><a href="#serialization-api">Serialization API</a> 136<ul> 137<li><a href="#v8serializevalue"><code>v8.serialize(value)</code></a></li> 138<li><a href="#v8deserializebuffer"><code>v8.deserialize(buffer)</code></a></li> 139<li><a href="#class-v8serializer">Class: <code>v8.Serializer</code></a> 140<ul> 141<li><a href="#new-serializer"><code>new Serializer()</code></a></li> 142<li><a href="#serializerwriteheader"><code>serializer.writeHeader()</code></a></li> 143<li><a href="#serializerwritevaluevalue"><code>serializer.writeValue(value)</code></a></li> 144<li><a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a></li> 145<li><a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 146<li><a href="#serializerwriteuint32value"><code>serializer.writeUint32(value)</code></a></li> 147<li><a href="#serializerwriteuint64hi-lo"><code>serializer.writeUint64(hi, lo)</code></a></li> 148<li><a href="#serializerwritedoublevalue"><code>serializer.writeDouble(value)</code></a></li> 149<li><a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes(buffer)</code></a></li> 150<li><a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject(object)</code></a></li> 151<li><a href="#serializer_getdatacloneerrormessage"><code>serializer._getDataCloneError(message)</code></a></li> 152<li><a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li> 153<li><a href="#serializer_settreatarraybufferviewsashostobjectsflag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li> 154</ul> 155</li> 156<li><a href="#class-v8deserializer">Class: <code>v8.Deserializer</code></a> 157<ul> 158<li><a href="#new-deserializerbuffer"><code>new Deserializer(buffer)</code></a></li> 159<li><a href="#deserializerreadheader"><code>deserializer.readHeader()</code></a></li> 160<li><a href="#deserializerreadvalue"><code>deserializer.readValue()</code></a></li> 161<li><a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 162<li><a href="#deserializergetwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li> 163<li><a href="#deserializerreaduint32"><code>deserializer.readUint32()</code></a></li> 164<li><a href="#deserializerreaduint64"><code>deserializer.readUint64()</code></a></li> 165<li><a href="#deserializerreaddouble"><code>deserializer.readDouble()</code></a></li> 166<li><a href="#deserializerreadrawbyteslength"><code>deserializer.readRawBytes(length)</code></a></li> 167<li><a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li> 168</ul> 169</li> 170<li><a href="#class-v8defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li> 171<li><a href="#class-v8defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li> 172</ul> 173</li> 174<li><a href="#promise-hooks">Promise hooks</a> 175<ul> 176<li><a href="#promisehooksoninitinit"><code>promiseHooks.onInit(init)</code></a></li> 177<li><a href="#promisehooksonsettledsettled"><code>promiseHooks.onSettled(settled)</code></a></li> 178<li><a href="#promisehooksonbeforebefore"><code>promiseHooks.onBefore(before)</code></a></li> 179<li><a href="#promisehooksonafterafter"><code>promiseHooks.onAfter(after)</code></a></li> 180<li><a href="#promisehookscreatehookcallbacks"><code>promiseHooks.createHook(callbacks)</code></a></li> 181<li><a href="#hook-callbacks">Hook callbacks</a> 182<ul> 183<li><a href="#initpromise-parent"><code>init(promise, parent)</code></a></li> 184<li><a href="#beforepromise"><code>before(promise)</code></a></li> 185<li><a href="#afterpromise"><code>after(promise)</code></a></li> 186<li><a href="#settledpromise"><code>settled(promise)</code></a></li> 187</ul> 188</li> 189</ul> 190</li> 191<li><span class="stability_1"><a href="#startup-snapshot-api">Startup Snapshot API</a></span> 192<ul> 193<li><a href="#v8startupsnapshotaddserializecallbackcallback-data"><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code></a></li> 194<li><a href="#v8startupsnapshotadddeserializecallbackcallback-data"><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code></a></li> 195<li><a href="#v8startupsnapshotsetdeserializemainfunctioncallback-data"><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code></a></li> 196<li><a href="#v8startupsnapshotisbuildingsnapshot"><code>v8.startupSnapshot.isBuildingSnapshot()</code></a></li> 197</ul> 198</li> 199<li><a href="#class-v8gcprofiler">Class: <code>v8.GCProfiler</code></a> 200<ul> 201<li><a href="#new-v8gcprofiler"><code>new v8.GCProfiler()</code></a></li> 202<li><a href="#profilerstart"><code>profiler.start()</code></a></li> 203<li><a href="#profilerstop"><code>profiler.stop()</code></a></li> 204</ul> 205</li> 206</ul> 207</li> 208</ul></div></div> 209 </li> 210 211 212 <li class="picker-header"> 213 <a href="#"> 214 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 215 Index 216 </a> 217 218 <div class="picker"><ul> 219<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 220<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 221 222 <li> 223 <a href="index.html">Index</a> 224 </li> 225 </ul> 226 227<hr class="line"> 228<ul> 229<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 230<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 231<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 232<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 233<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 234<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 235<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 236<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 237<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 238<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 239<li><a href="console.html" class="nav-console">Console</a></li> 240<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 241<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 242<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 243<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 244<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 245<li><a href="dns.html" class="nav-dns">DNS</a></li> 246<li><a href="domain.html" class="nav-domain">Domain</a></li> 247<li><a href="errors.html" class="nav-errors">Errors</a></li> 248<li><a href="events.html" class="nav-events">Events</a></li> 249<li><a href="fs.html" class="nav-fs">File system</a></li> 250<li><a href="globals.html" class="nav-globals">Globals</a></li> 251<li><a href="http.html" class="nav-http">HTTP</a></li> 252<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 253<li><a href="https.html" class="nav-https">HTTPS</a></li> 254<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 255<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 256<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 257<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 258<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 259<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 260<li><a href="net.html" class="nav-net">Net</a></li> 261<li><a href="os.html" class="nav-os">OS</a></li> 262<li><a href="path.html" class="nav-path">Path</a></li> 263<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 264<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 265<li><a href="process.html" class="nav-process">Process</a></li> 266<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 267<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 268<li><a href="readline.html" class="nav-readline">Readline</a></li> 269<li><a href="repl.html" class="nav-repl">REPL</a></li> 270<li><a href="report.html" class="nav-report">Report</a></li> 271<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 272<li><a href="stream.html" class="nav-stream">Stream</a></li> 273<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 274<li><a href="test.html" class="nav-test">Test runner</a></li> 275<li><a href="timers.html" class="nav-timers">Timers</a></li> 276<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 277<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 278<li><a href="tty.html" class="nav-tty">TTY</a></li> 279<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 280<li><a href="url.html" class="nav-url">URL</a></li> 281<li><a href="util.html" class="nav-util">Utilities</a></li> 282<li><a href="v8.html" class="nav-v8 active">V8</a></li> 283<li><a href="vm.html" class="nav-vm">VM</a></li> 284<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 285<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 286<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 287<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 288<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 289</ul> 290<hr class="line"> 291<ul> 292<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 293</ul></div> 294 </li> 295 296 297 <li class="picker-header"> 298 <a href="#"> 299 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 300 Other versions 301 </a> 302 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/v8.html">20.x</a></li> 303<li><a href="https://nodejs.org/docs/latest-v19.x/api/v8.html">19.x</a></li> 304<li><a href="https://nodejs.org/docs/latest-v18.x/api/v8.html">18.x <b>LTS</b></a></li> 305<li><a href="https://nodejs.org/docs/latest-v17.x/api/v8.html">17.x</a></li> 306<li><a href="https://nodejs.org/docs/latest-v16.x/api/v8.html">16.x</a></li> 307<li><a href="https://nodejs.org/docs/latest-v15.x/api/v8.html">15.x</a></li> 308<li><a href="https://nodejs.org/docs/latest-v14.x/api/v8.html">14.x</a></li> 309<li><a href="https://nodejs.org/docs/latest-v13.x/api/v8.html">13.x</a></li> 310<li><a href="https://nodejs.org/docs/latest-v12.x/api/v8.html">12.x</a></li> 311<li><a href="https://nodejs.org/docs/latest-v11.x/api/v8.html">11.x</a></li> 312<li><a href="https://nodejs.org/docs/latest-v10.x/api/v8.html">10.x</a></li> 313<li><a href="https://nodejs.org/docs/latest-v9.x/api/v8.html">9.x</a></li> 314<li><a href="https://nodejs.org/docs/latest-v8.x/api/v8.html">8.x</a></li> 315<li><a href="https://nodejs.org/docs/latest-v7.x/api/v8.html">7.x</a></li> 316<li><a href="https://nodejs.org/docs/latest-v6.x/api/v8.html">6.x</a></li> 317<li><a href="https://nodejs.org/docs/latest-v5.x/api/v8.html">5.x</a></li> 318<li><a href="https://nodejs.org/docs/latest-v4.x/api/v8.html">4.x</a></li></ol></div> 319 </li> 320 321 <li class="picker-header"> 322 <a href="#"> 323 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 324 Options 325 </a> 326 327 <div class="picker"> 328 <ul> 329 <li> 330 <a href="all.html">View on single page</a> 331 </li> 332 <li> 333 <a href="v8.json">View as JSON</a> 334 </li> 335 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/v8.md">Edit on GitHub</a></li> 336 </ul> 337 </div> 338 </li> 339 </ul> 340 </div> 341 <hr> 342 </header> 343 344 <details id="toc" open><summary>Table of contents</summary><ul> 345<li><a href="#v8">V8</a> 346<ul> 347<li><a href="#v8cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li> 348<li><a href="#v8getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li> 349<li><a href="#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li> 350<li><a href="#v8getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li> 351<li><a href="#v8getheapstatistics"><code>v8.getHeapStatistics()</code></a></li> 352<li><a href="#v8setflagsfromstringflags"><code>v8.setFlagsFromString(flags)</code></a></li> 353<li><a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a></li> 354<li><a href="#v8takecoverage"><code>v8.takeCoverage()</code></a></li> 355<li><a href="#v8writeheapsnapshotfilename"><code>v8.writeHeapSnapshot([filename])</code></a></li> 356<li><span class="stability_1"><a href="#v8setheapsnapshotnearheaplimitlimit"><code>v8.setHeapSnapshotNearHeapLimit(limit)</code></a></span></li> 357<li><a href="#serialization-api">Serialization API</a> 358<ul> 359<li><a href="#v8serializevalue"><code>v8.serialize(value)</code></a></li> 360<li><a href="#v8deserializebuffer"><code>v8.deserialize(buffer)</code></a></li> 361<li><a href="#class-v8serializer">Class: <code>v8.Serializer</code></a> 362<ul> 363<li><a href="#new-serializer"><code>new Serializer()</code></a></li> 364<li><a href="#serializerwriteheader"><code>serializer.writeHeader()</code></a></li> 365<li><a href="#serializerwritevaluevalue"><code>serializer.writeValue(value)</code></a></li> 366<li><a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a></li> 367<li><a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 368<li><a href="#serializerwriteuint32value"><code>serializer.writeUint32(value)</code></a></li> 369<li><a href="#serializerwriteuint64hi-lo"><code>serializer.writeUint64(hi, lo)</code></a></li> 370<li><a href="#serializerwritedoublevalue"><code>serializer.writeDouble(value)</code></a></li> 371<li><a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes(buffer)</code></a></li> 372<li><a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject(object)</code></a></li> 373<li><a href="#serializer_getdatacloneerrormessage"><code>serializer._getDataCloneError(message)</code></a></li> 374<li><a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li> 375<li><a href="#serializer_settreatarraybufferviewsashostobjectsflag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li> 376</ul> 377</li> 378<li><a href="#class-v8deserializer">Class: <code>v8.Deserializer</code></a> 379<ul> 380<li><a href="#new-deserializerbuffer"><code>new Deserializer(buffer)</code></a></li> 381<li><a href="#deserializerreadheader"><code>deserializer.readHeader()</code></a></li> 382<li><a href="#deserializerreadvalue"><code>deserializer.readValue()</code></a></li> 383<li><a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 384<li><a href="#deserializergetwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li> 385<li><a href="#deserializerreaduint32"><code>deserializer.readUint32()</code></a></li> 386<li><a href="#deserializerreaduint64"><code>deserializer.readUint64()</code></a></li> 387<li><a href="#deserializerreaddouble"><code>deserializer.readDouble()</code></a></li> 388<li><a href="#deserializerreadrawbyteslength"><code>deserializer.readRawBytes(length)</code></a></li> 389<li><a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li> 390</ul> 391</li> 392<li><a href="#class-v8defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li> 393<li><a href="#class-v8defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li> 394</ul> 395</li> 396<li><a href="#promise-hooks">Promise hooks</a> 397<ul> 398<li><a href="#promisehooksoninitinit"><code>promiseHooks.onInit(init)</code></a></li> 399<li><a href="#promisehooksonsettledsettled"><code>promiseHooks.onSettled(settled)</code></a></li> 400<li><a href="#promisehooksonbeforebefore"><code>promiseHooks.onBefore(before)</code></a></li> 401<li><a href="#promisehooksonafterafter"><code>promiseHooks.onAfter(after)</code></a></li> 402<li><a href="#promisehookscreatehookcallbacks"><code>promiseHooks.createHook(callbacks)</code></a></li> 403<li><a href="#hook-callbacks">Hook callbacks</a> 404<ul> 405<li><a href="#initpromise-parent"><code>init(promise, parent)</code></a></li> 406<li><a href="#beforepromise"><code>before(promise)</code></a></li> 407<li><a href="#afterpromise"><code>after(promise)</code></a></li> 408<li><a href="#settledpromise"><code>settled(promise)</code></a></li> 409</ul> 410</li> 411</ul> 412</li> 413<li><span class="stability_1"><a href="#startup-snapshot-api">Startup Snapshot API</a></span> 414<ul> 415<li><a href="#v8startupsnapshotaddserializecallbackcallback-data"><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code></a></li> 416<li><a href="#v8startupsnapshotadddeserializecallbackcallback-data"><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code></a></li> 417<li><a href="#v8startupsnapshotsetdeserializemainfunctioncallback-data"><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code></a></li> 418<li><a href="#v8startupsnapshotisbuildingsnapshot"><code>v8.startupSnapshot.isBuildingSnapshot()</code></a></li> 419</ul> 420</li> 421<li><a href="#class-v8gcprofiler">Class: <code>v8.GCProfiler</code></a> 422<ul> 423<li><a href="#new-v8gcprofiler"><code>new v8.GCProfiler()</code></a></li> 424<li><a href="#profilerstart"><code>profiler.start()</code></a></li> 425<li><a href="#profilerstop"><code>profiler.stop()</code></a></li> 426</ul> 427</li> 428</ul> 429</li> 430</ul></details> 431 432 <div id="apicontent"> 433 <h2>V8<span><a class="mark" href="#v8" id="v8">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8"></a></h2> 434 435<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/v8.js">lib/v8.js</a></p> 436<p>The <code>node:v8</code> module exposes APIs that are specific to the version of <a href="https://developers.google.com/v8/">V8</a> 437built into the Node.js binary. It can be accessed using:</p> 438<pre><code class="language-js"><span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>);</code> <button class="copy-button">copy</button></pre> 439<section><h3><code>v8.cachedDataVersionTag()</code><span><a class="mark" href="#v8cacheddataversiontag" id="v8cacheddataversiontag">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_cacheddataversiontag"></a></h3> 440<div class="api_metadata"> 441<span>Added in: v8.0.0</span> 442</div> 443<ul> 444<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 445</ul> 446<p>Returns an integer representing a version tag derived from the V8 version, 447command-line flags, and detected CPU features. This is useful for determining 448whether a <a href="vm.html#new-vmscriptcode-options"><code>vm.Script</code></a> <code>cachedData</code> buffer is compatible with this instance 449of V8.</p> 450<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(v8.<span class="hljs-title function_">cachedDataVersionTag</span>()); <span class="hljs-comment">// 3947234607</span> 451<span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V8</span> 452<span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the value</span> 453<span class="hljs-comment">// does indeed update when flags are toggled.</span> 454v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_natives_syntax'</span>); 455<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(v8.<span class="hljs-title function_">cachedDataVersionTag</span>()); <span class="hljs-comment">// 183726201</span></code> <button class="copy-button">copy</button></pre> 456</section><section><h3><code>v8.getHeapCodeStatistics()</code><span><a class="mark" href="#v8getheapcodestatistics" id="v8getheapcodestatistics">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_getheapcodestatistics"></a></h3> 457<div class="api_metadata"> 458<span>Added in: v12.8.0</span> 459</div> 460<ul> 461<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 462</ul> 463<p>Get statistics about code and its metadata in the heap, see V8 464<a href="https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#a6079122af17612ef54ef3348ce170866"><code>GetHeapCodeAndMetadataStatistics</code></a> API. Returns an object with the 465following properties:</p> 466<ul> 467<li><code>code_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 468<li><code>bytecode_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 469<li><code>external_script_source_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 470<li><code>cpu_profiler_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 471</ul> 472<!-- eslint-skip --> 473<pre><code class="language-js">{ 474 <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>, 475 <span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>, 476 <span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</span>, 477 <span class="hljs-attr">cpu_profiler_metadata_size</span>: <span class="hljs-number">0</span>, 478}</code> <button class="copy-button">copy</button></pre> 479</section><section><h3><code>v8.getHeapSnapshot()</code><span><a class="mark" href="#v8getheapsnapshot" id="v8getheapsnapshot">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_getheapsnapshot"></a></h3> 480<div class="api_metadata"> 481<span>Added in: v11.13.0</span> 482</div> 483<ul> 484<li>Returns: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> A Readable Stream containing the V8 heap snapshot</li> 485</ul> 486<p>Generates a snapshot of the current V8 heap and returns a Readable 487Stream that may be used to read the JSON serialized representation. 488This JSON stream format is intended to be used with tools such as 489Chrome DevTools. The JSON schema is undocumented and specific to the 490V8 engine. Therefore, the schema may change from one version of V8 to the next.</p> 491<p>Creating a heap snapshot requires memory about twice the size of the heap at 492the time the snapshot is created. This results in the risk of OOM killers 493terminating the process.</p> 494<p>Generating a snapshot is a synchronous operation which blocks the event loop 495for a duration depending on the heap size.</p> 496<pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</span> 497<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 498<span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnapshot</span>(); 499stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code> <button class="copy-button">copy</button></pre> 500</section><section><h3><code>v8.getHeapSpaceStatistics()</code><span><a class="mark" href="#v8getheapspacestatistics" id="v8getheapspacestatistics">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_getheapspacestatistics"></a></h3> 501<div class="api_metadata"> 502<details class="changelog"><summary>History</summary> 503<table> 504<tbody><tr><th>Version</th><th>Changes</th></tr> 505<tr><td>v7.5.0</td> 506<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr> 507<tr><td>v6.0.0</td> 508<td><p><span>Added in: v6.0.0</span></p></td></tr> 509</tbody></table> 510</details> 511</div> 512<ul> 513<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a></li> 514</ul> 515<p>Returns statistics about the V8 heap spaces, i.e. the segments which make up 516the V8 heap. Neither the ordering of heap spaces, nor the availability of a 517heap space can be guaranteed as the statistics are provided via the V8 518<a href="https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4"><code>GetHeapSpaceStatistics</code></a> function and may change from one V8 version to the 519next.</p> 520<p>The value returned is an array of objects containing the following properties:</p> 521<ul> 522<li><code>space_name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 523<li><code>space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 524<li><code>space_used_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 525<li><code>space_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 526<li><code>physical_space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 527</ul> 528<pre><code class="language-json"><span class="hljs-punctuation">[</span> 529 <span class="hljs-punctuation">{</span> 530 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"new_space"</span><span class="hljs-punctuation">,</span> 531 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span><span class="hljs-punctuation">,</span> 532 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">951112</span><span class="hljs-punctuation">,</span> 533 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">80824</span><span class="hljs-punctuation">,</span> 534 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span> 535 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 536 <span class="hljs-punctuation">{</span> 537 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"old_space"</span><span class="hljs-punctuation">,</span> 538 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span><span class="hljs-punctuation">,</span> 539 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2493792</span><span class="hljs-punctuation">,</span> 540 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 541 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span> 542 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 543 <span class="hljs-punctuation">{</span> 544 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"code_space"</span><span class="hljs-punctuation">,</span> 545 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span><span class="hljs-punctuation">,</span> 546 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">644256</span><span class="hljs-punctuation">,</span> 547 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">960</span><span class="hljs-punctuation">,</span> 548 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span> 549 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 550 <span class="hljs-punctuation">{</span> 551 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"map_space"</span><span class="hljs-punctuation">,</span> 552 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span><span class="hljs-punctuation">,</span> 553 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">201608</span><span class="hljs-punctuation">,</span> 554 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 555 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span> 556 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 557 <span class="hljs-punctuation">{</span> 558 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"large_object_space"</span><span class="hljs-punctuation">,</span> 559 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 560 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 561 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1490980608</span><span class="hljs-punctuation">,</span> 562 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span> 563 <span class="hljs-punctuation">}</span> 564<span class="hljs-punctuation">]</span></code> <button class="copy-button">copy</button></pre> 565</section><section><h3><code>v8.getHeapStatistics()</code><span><a class="mark" href="#v8getheapstatistics" id="v8getheapstatistics">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_getheapstatistics"></a></h3> 566<div class="api_metadata"> 567<details class="changelog"><summary>History</summary> 568<table> 569<tbody><tr><th>Version</th><th>Changes</th></tr> 570<tr><td>v7.5.0</td> 571<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr> 572<tr><td>v7.2.0</td> 573<td><p>Added <code>malloced_memory</code>, <code>peak_malloced_memory</code>, and <code>does_zap_garbage</code>.</p></td></tr> 574<tr><td>v1.0.0</td> 575<td><p><span>Added in: v1.0.0</span></p></td></tr> 576</tbody></table> 577</details> 578</div> 579<ul> 580<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 581</ul> 582<p>Returns an object with the following properties:</p> 583<ul> 584<li><code>total_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 585<li><code>total_heap_size_executable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 586<li><code>total_physical_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 587<li><code>total_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 588<li><code>used_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 589<li><code>heap_size_limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 590<li><code>malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 591<li><code>peak_malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 592<li><code>does_zap_garbage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 593<li><code>number_of_native_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 594<li><code>number_of_detached_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 595<li><code>total_global_handles_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 596<li><code>used_global_handles_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 597<li><code>external_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 598</ul> 599<p><code>does_zap_garbage</code> is a 0/1 boolean, which signifies whether the 600<code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap 601garbage with a bit pattern. The RSS footprint (resident set size) gets bigger 602because it continuously touches all heap pages and that makes them less likely 603to get swapped out by the operating system.</p> 604<p><code>number_of_native_contexts</code> The value of native_context is the number of the 605top-level contexts currently active. Increase of this number over time indicates 606a memory leak.</p> 607<p><code>number_of_detached_contexts</code> The value of detached_context is the number 608of contexts that were detached and not yet garbage collected. This number 609being non-zero indicates a potential memory leak.</p> 610<p><code>total_global_handles_size</code> The value of total_global_handles_size is the 611total memory size of V8 global handles.</p> 612<p><code>used_global_handles_size</code> The value of used_global_handles_size is the 613used memory size of V8 global handles.</p> 614<p><code>external_memory</code> The value of external_memory is the memory size of array 615buffers and external strings.</p> 616<!-- eslint-skip --> 617<pre><code class="language-js">{ 618 <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>, 619 <span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span>, 620 <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>, 621 <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>, 622 <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>, 623 <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>, 624 <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>, 625 <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>, 626 <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>, 627 <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>, 628 <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>, 629 <span class="hljs-attr">total_global_handles_size</span>: <span class="hljs-number">8192</span>, 630 <span class="hljs-attr">used_global_handles_size</span>: <span class="hljs-number">3296</span>, 631 <span class="hljs-attr">external_memory</span>: <span class="hljs-number">318824</span> 632}</code> <button class="copy-button">copy</button></pre> 633</section><section><h3><code>v8.setFlagsFromString(flags)</code><span><a class="mark" href="#v8setflagsfromstringflags" id="v8setflagsfromstringflags">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_setflagsfromstring_flags"></a></h3> 634<div class="api_metadata"> 635<span>Added in: v1.0.0</span> 636</div> 637<ul> 638<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 639</ul> 640<p>The <code>v8.setFlagsFromString()</code> method can be used to programmatically set 641V8 command-line flags. This method should be used with care. Changing settings 642after the VM has started may result in unpredictable behavior, including 643crashes and data loss; or it may simply do nothing.</p> 644<p>The V8 options available for a version of Node.js may be determined by running 645<code>node --v8-options</code>.</p> 646<p>Usage:</p> 647<pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minute.</span> 648<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 649v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_gc'</span>); 650<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--notrace_gc'</span>); }, <span class="hljs-number">60e3</span>);</code> <button class="copy-button">copy</button></pre> 651</section><section><h3><code>v8.stopCoverage()</code><span><a class="mark" href="#v8stopcoverage" id="v8stopcoverage">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_stopcoverage"></a></h3> 652<div class="api_metadata"> 653<span>Added in: v15.1.0, v14.18.0, v12.22.0</span> 654</div> 655<p>The <code>v8.stopCoverage()</code> method allows the user to stop the coverage collection 656started by <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>, so that V8 can release the execution count 657records and optimize code. This can be used in conjunction with 658<a href="#v8takecoverage"><code>v8.takeCoverage()</code></a> if the user wants to collect the coverage on demand.</p> 659</section><section><h3><code>v8.takeCoverage()</code><span><a class="mark" href="#v8takecoverage" id="v8takecoverage">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_takecoverage"></a></h3> 660<div class="api_metadata"> 661<span>Added in: v15.1.0, v14.18.0, v12.22.0</span> 662</div> 663<p>The <code>v8.takeCoverage()</code> method allows the user to write the coverage started by 664<a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> to disk on demand. This method can be invoked multiple 665times during the lifetime of the process. Each time the execution counter will 666be reset and a new coverage report will be written to the directory specified 667by <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>.</p> 668<p>When the process is about to exit, one last coverage will still be written to 669disk unless <a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a> is invoked before the process exits.</p> 670</section><section><h3><code>v8.writeHeapSnapshot([filename])</code><span><a class="mark" href="#v8writeheapsnapshotfilename" id="v8writeheapsnapshotfilename">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_writeheapsnapshot_filename"></a></h3> 671<div class="api_metadata"> 672<details class="changelog"><summary>History</summary> 673<table> 674<tbody><tr><th>Version</th><th>Changes</th></tr> 675<tr><td>v18.0.0</td> 676<td><p>An exception will now be thrown if the file could not be written.</p></td></tr> 677<tr><td>v18.0.0</td> 678<td><p>Make the returned error codes consistent across all platforms.</p></td></tr> 679<tr><td>v11.13.0</td> 680<td><p><span>Added in: v11.13.0</span></p></td></tr> 681</tbody></table> 682</details> 683</div> 684<ul> 685<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The file path where the V8 heap snapshot is to be 686saved. If not specified, a file name with the pattern 687<code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be 688generated, where <code>{pid}</code> will be the PID of the Node.js process, 689<code>{thread_id}</code> will be <code>0</code> when <code>writeHeapSnapshot()</code> is called from 690the main Node.js thread or the id of a worker thread.</li> 691<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The filename where the snapshot was saved.</li> 692</ul> 693<p>Generates a snapshot of the current V8 heap and writes it to a JSON 694file. This file is intended to be used with tools such as Chrome 695DevTools. The JSON schema is undocumented and specific to the V8 696engine, and may change from one version of V8 to the next.</p> 697<p>A heap snapshot is specific to a single V8 isolate. When using 698<a href="worker_threads.html">worker threads</a>, a heap snapshot generated from the main thread will 699not contain any information about the workers, and vice versa.</p> 700<p>Creating a heap snapshot requires memory about twice the size of the heap at 701the time the snapshot is created. This results in the risk of OOM killers 702terminating the process.</p> 703<p>Generating a snapshot is a synchronous operation which blocks the event loop 704for a duration depending on the heap size.</p> 705<pre><code class="language-js"><span class="hljs-keyword">const</span> { writeHeapSnapshot } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 706<span class="hljs-keyword">const</span> { 707 <span class="hljs-title class_">Worker</span>, 708 isMainThread, 709 parentPort, 710} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 711 712<span class="hljs-keyword">if</span> (isMainThread) { 713 <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 714 715 worker.<span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">filename</span>) =></span> { 716 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker heapdump: <span class="hljs-subst">${filename}</span>`</span>); 717 <span class="hljs-comment">// Now get a heapdump for the main thread.</span> 718 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`main thread heapdump: <span class="hljs-subst">${writeHeapSnapshot()}</span>`</span>); 719 }); 720 721 <span class="hljs-comment">// Tell the worker to create a heapdump.</span> 722 worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'</span>); 723} <span class="hljs-keyword">else</span> { 724 parentPort.<span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> { 725 <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) { 726 <span class="hljs-comment">// Generate a heapdump for the worker</span> 727 <span class="hljs-comment">// and return the filename to the parent.</span> 728 parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">writeHeapSnapshot</span>()); 729 } 730 }); 731}</code> <button class="copy-button">copy</button></pre> 732</section><section><h3><code>v8.setHeapSnapshotNearHeapLimit(limit)</code><span><a class="mark" href="#v8setheapsnapshotnearheaplimitlimit" id="v8setheapsnapshotnearheaplimitlimit">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_setheapsnapshotnearheaplimit_limit"></a></h3> 733<div class="api_metadata"> 734<span>Added in: v18.10.0</span> 735</div> 736<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 737<ul> 738<li><code>limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 739</ul> 740<p>The API is a no-op if <code>--heapsnapshot-near-heap-limit</code> is already set from the 741command line or the API is called more than once. <code>limit</code> must be a positive 742integer. See <a href="cli.html#--heapsnapshot-near-heap-limitmax_count"><code>--heapsnapshot-near-heap-limit</code></a> for more information.</p> 743</section><section><h3>Serialization API<span><a class="mark" href="#serialization-api" id="serialization-api">#</a></span><a aria-hidden="true" class="legacy" id="v8_serialization_api"></a></h3> 744<p>The serialization API provides means of serializing JavaScript values in a way 745that is compatible with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>.</p> 746<p>The format is backward-compatible (i.e. safe to store to disk). 747Equal JavaScript values may result in different serialized output.</p> 748<h4><code>v8.serialize(value)</code><span><a class="mark" href="#v8serializevalue" id="v8serializevalue">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_serialize_value"></a></h4> 749<div class="api_metadata"> 750<span>Added in: v8.0.0</span> 751</div> 752<ul> 753<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 754<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 755</ul> 756<p>Uses a <a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a> to serialize <code>value</code> into a buffer.</p> 757<p><a href="errors.html#err_buffer_too_large"><code>ERR_BUFFER_TOO_LARGE</code></a> will be thrown when trying to 758serialize a huge object which requires buffer 759larger than <a href="buffer.html#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> 760<h4><code>v8.deserialize(buffer)</code><span><a class="mark" href="#v8deserializebuffer" id="v8deserializebuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_deserialize_buffer"></a></h4> 761<div class="api_metadata"> 762<span>Added in: v8.0.0</span> 763</div> 764<ul> 765<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A buffer returned by <a href="#v8serializevalue"><code>serialize()</code></a>.</li> 766</ul> 767<p>Uses a <a href="#class-v8defaultdeserializer"><code>DefaultDeserializer</code></a> with default options to read a JS value 768from a buffer.</p> 769<h4>Class: <code>v8.Serializer</code><span><a class="mark" href="#class-v8serializer" id="class-v8serializer">#</a></span><a aria-hidden="true" class="legacy" id="v8_class_v8_serializer"></a></h4> 770<div class="api_metadata"> 771<span>Added in: v8.0.0</span> 772</div> 773<h5><code>new Serializer()</code><span><a class="mark" href="#new-serializer" id="new-serializer">#</a></span><a aria-hidden="true" class="legacy" id="v8_new_serializer"></a></h5> 774<p>Creates a new <code>Serializer</code> object.</p> 775<h5><code>serializer.writeHeader()</code><span><a class="mark" href="#serializerwriteheader" id="serializerwriteheader">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writeheader"></a></h5> 776<p>Writes out a header, which includes the serialization format version.</p> 777<h5><code>serializer.writeValue(value)</code><span><a class="mark" href="#serializerwritevaluevalue" id="serializerwritevaluevalue">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writevalue_value"></a></h5> 778<ul> 779<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 780</ul> 781<p>Serializes a JavaScript value and adds the serialized representation to the 782internal buffer.</p> 783<p>This throws an error if <code>value</code> cannot be serialized.</p> 784<h5><code>serializer.releaseBuffer()</code><span><a class="mark" href="#serializerreleasebuffer" id="serializerreleasebuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_releasebuffer"></a></h5> 785<ul> 786<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 787</ul> 788<p>Returns the stored internal buffer. This serializer should not be used once 789the buffer is released. Calling this method results in undefined behavior 790if a previous write has failed.</p> 791<h5><code>serializer.transferArrayBuffer(id, arrayBuffer)</code><span><a class="mark" href="#serializertransferarraybufferid-arraybuffer" id="serializertransferarraybufferid-arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_transferarraybuffer_id_arraybuffer"></a></h5> 792<ul> 793<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> A 32-bit unsigned integer.</li> 794<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> An <code>ArrayBuffer</code> instance.</li> 795</ul> 796<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band. 797Pass the corresponding <code>ArrayBuffer</code> in the deserializing context to 798<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p> 799<h5><code>serializer.writeUint32(value)</code><span><a class="mark" href="#serializerwriteuint32value" id="serializerwriteuint32value">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writeuint32_value"></a></h5> 800<ul> 801<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 802</ul> 803<p>Write a raw 32-bit unsigned integer. 804For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 805<h5><code>serializer.writeUint64(hi, lo)</code><span><a class="mark" href="#serializerwriteuint64hi-lo" id="serializerwriteuint64hi-lo">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writeuint64_hi_lo"></a></h5> 806<ul> 807<li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 808<li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 809</ul> 810<p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts. 811For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 812<h5><code>serializer.writeDouble(value)</code><span><a class="mark" href="#serializerwritedoublevalue" id="serializerwritedoublevalue">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writedouble_value"></a></h5> 813<ul> 814<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 815</ul> 816<p>Write a JS <code>number</code> value. 817For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 818<h5><code>serializer.writeRawBytes(buffer)</code><span><a class="mark" href="#serializerwriterawbytesbuffer" id="serializerwriterawbytesbuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writerawbytes_buffer"></a></h5> 819<ul> 820<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 821</ul> 822<p>Write raw bytes into the serializer's internal buffer. The deserializer 823will require a way to compute the length of the buffer. 824For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 825<h5><code>serializer._writeHostObject(object)</code><span><a class="mark" href="#serializer_writehostobjectobject" id="serializer_writehostobjectobject">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_writehostobject_object"></a></h5> 826<ul> 827<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 828</ul> 829<p>This method is called to write some kind of host object, i.e. an object created 830by native C++ bindings. If it is not possible to serialize <code>object</code>, a suitable 831exception should be thrown.</p> 832<p>This method is not present on the <code>Serializer</code> class itself but can be provided 833by subclasses.</p> 834<h5><code>serializer._getDataCloneError(message)</code><span><a class="mark" href="#serializer_getdatacloneerrormessage" id="serializer_getdatacloneerrormessage">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_getdatacloneerror_message"></a></h5> 835<ul> 836<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 837</ul> 838<p>This method is called to generate error objects that will be thrown when an 839object can not be cloned.</p> 840<p>This method defaults to the <a href="errors.html#class-error"><code>Error</code></a> constructor and can be overridden on 841subclasses.</p> 842<h5><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code><span><a class="mark" href="#serializer_getsharedarraybufferidsharedarraybuffer" id="serializer_getsharedarraybufferidsharedarraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_getsharedarraybufferid_sharedarraybuffer"></a></h5> 843<ul> 844<li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a></li> 845</ul> 846<p>This method is called when the serializer is going to serialize a 847<code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for 848the object, using the same ID if this <code>SharedArrayBuffer</code> has already been 849serialized. When deserializing, this ID will be passed to 850<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p> 851<p>If the object cannot be serialized, an exception should be thrown.</p> 852<p>This method is not present on the <code>Serializer</code> class itself but can be provided 853by subclasses.</p> 854<h5><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code><span><a class="mark" href="#serializer_settreatarraybufferviewsashostobjectsflag" id="serializer_settreatarraybufferviewsashostobjectsflag">#</a></span><a aria-hidden="true" class="legacy" id="v8_serializer_settreatarraybufferviewsashostobjects_flag"></a></h5> 855<ul> 856<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 857</ul> 858<p>Indicate whether to treat <code>TypedArray</code> and <code>DataView</code> objects as 859host objects, i.e. pass them to <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 860<h4>Class: <code>v8.Deserializer</code><span><a class="mark" href="#class-v8deserializer" id="class-v8deserializer">#</a></span><a aria-hidden="true" class="legacy" id="v8_class_v8_deserializer"></a></h4> 861<div class="api_metadata"> 862<span>Added in: v8.0.0</span> 863</div> 864<h5><code>new Deserializer(buffer)</code><span><a class="mark" href="#new-deserializerbuffer" id="new-deserializerbuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_new_deserializer_buffer"></a></h5> 865<ul> 866<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A buffer returned by 867<a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a>.</li> 868</ul> 869<p>Creates a new <code>Deserializer</code> object.</p> 870<h5><code>deserializer.readHeader()</code><span><a class="mark" href="#deserializerreadheader" id="deserializerreadheader">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readheader"></a></h5> 871<p>Reads and validates a header (including the format version). 872May, for example, reject an invalid or unsupported wire format. In that case, 873an <code>Error</code> is thrown.</p> 874<h5><code>deserializer.readValue()</code><span><a class="mark" href="#deserializerreadvalue" id="deserializerreadvalue">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readvalue"></a></h5> 875<p>Deserializes a JavaScript value from the buffer and returns it.</p> 876<h5><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code><span><a class="mark" href="#deserializertransferarraybufferid-arraybuffer" id="deserializertransferarraybufferid-arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_transferarraybuffer_id_arraybuffer"></a></h5> 877<ul> 878<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> A 32-bit unsigned integer.</li> 879<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> An <code>ArrayBuffer</code> instance.</li> 880</ul> 881<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band. 882Pass the corresponding <code>ArrayBuffer</code> in the serializing context to 883<a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer()</code></a> (or return the <code>id</code> from 884<a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId()</code></a> in the case of <code>SharedArrayBuffer</code>s).</p> 885<h5><code>deserializer.getWireFormatVersion()</code><span><a class="mark" href="#deserializergetwireformatversion" id="deserializergetwireformatversion">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_getwireformatversion"></a></h5> 886<ul> 887<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 888</ul> 889<p>Reads the underlying wire format version. Likely mostly to be useful to 890legacy code reading old wire format versions. May not be called before 891<code>.readHeader()</code>.</p> 892<h5><code>deserializer.readUint32()</code><span><a class="mark" href="#deserializerreaduint32" id="deserializerreaduint32">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readuint32"></a></h5> 893<ul> 894<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 895</ul> 896<p>Read a raw 32-bit unsigned integer and return it. 897For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 898<h5><code>deserializer.readUint64()</code><span><a class="mark" href="#deserializerreaduint64" id="deserializerreaduint64">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readuint64"></a></h5> 899<ul> 900<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 901</ul> 902<p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code> 903with two 32-bit unsigned integer entries. 904For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 905<h5><code>deserializer.readDouble()</code><span><a class="mark" href="#deserializerreaddouble" id="deserializerreaddouble">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readdouble"></a></h5> 906<ul> 907<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 908</ul> 909<p>Read a JS <code>number</code> value. 910For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 911<h5><code>deserializer.readRawBytes(length)</code><span><a class="mark" href="#deserializerreadrawbyteslength" id="deserializerreadrawbyteslength">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readrawbytes_length"></a></h5> 912<ul> 913<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 914<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 915</ul> 916<p>Read raw bytes from the deserializer's internal buffer. The <code>length</code> parameter 917must correspond to the length of the buffer that was passed to 918<a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes()</code></a>. 919For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 920<h5><code>deserializer._readHostObject()</code><span><a class="mark" href="#deserializer_readhostobject" id="deserializer_readhostobject">#</a></span><a aria-hidden="true" class="legacy" id="v8_deserializer_readhostobject"></a></h5> 921<p>This method is called to read some kind of host object, i.e. an object that is 922created by native C++ bindings. If it is not possible to deserialize the data, 923a suitable exception should be thrown.</p> 924<p>This method is not present on the <code>Deserializer</code> class itself but can be 925provided by subclasses.</p> 926<h4>Class: <code>v8.DefaultSerializer</code><span><a class="mark" href="#class-v8defaultserializer" id="class-v8defaultserializer">#</a></span><a aria-hidden="true" class="legacy" id="v8_class_v8_defaultserializer"></a></h4> 927<div class="api_metadata"> 928<span>Added in: v8.0.0</span> 929</div> 930<p>A subclass of <a href="#class-v8serializer"><code>Serializer</code></a> that serializes <code>TypedArray</code> 931(in particular <a href="buffer.html"><code>Buffer</code></a>) and <code>DataView</code> objects as host objects, and only 932stores the part of their underlying <code>ArrayBuffer</code>s that they are referring to.</p> 933<h4>Class: <code>v8.DefaultDeserializer</code><span><a class="mark" href="#class-v8defaultdeserializer" id="class-v8defaultdeserializer">#</a></span><a aria-hidden="true" class="legacy" id="v8_class_v8_defaultdeserializer"></a></h4> 934<div class="api_metadata"> 935<span>Added in: v8.0.0</span> 936</div> 937<p>A subclass of <a href="#class-v8deserializer"><code>Deserializer</code></a> corresponding to the format written by 938<a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a>.</p> 939</section><section><h3>Promise hooks<span><a class="mark" href="#promise-hooks" id="promise-hooks">#</a></span><a aria-hidden="true" class="legacy" id="v8_promise_hooks"></a></h3> 940<p>The <code>promiseHooks</code> interface can be used to track promise lifecycle events. 941To track <em>all</em> async activity, see <a href="async_hooks.html"><code>async_hooks</code></a> which internally uses this 942module to produce promise lifecycle events in addition to events for other 943async resources. For request context management, see <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.</p> 944<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 945 946<span class="hljs-comment">// There are four lifecycle events produced by promises:</span> 947 948<span class="hljs-comment">// The `init` event represents the creation of a promise. This could be a</span> 949<span class="hljs-comment">// direct creation such as with `new Promise(...)` or a continuation such</span> 950<span class="hljs-comment">// as `then()` or `catch()`. It also happens whenever an async function is</span> 951<span class="hljs-comment">// called or does an `await`. If a continuation promise is created, the</span> 952<span class="hljs-comment">// `parent` will be the promise it is a continuation from.</span> 953<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) { 954 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a promise was created'</span>, { promise, parent }); 955} 956 957<span class="hljs-comment">// The `settled` event happens when a promise receives a resolution or</span> 958<span class="hljs-comment">// rejection value. This may happen synchronously such as when using</span> 959<span class="hljs-comment">// `Promise.resolve()` on non-promise input.</span> 960<span class="hljs-keyword">function</span> <span class="hljs-title function_">settled</span>(<span class="hljs-params">promise</span>) { 961 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a promise resolved or rejected'</span>, { promise }); 962} 963 964<span class="hljs-comment">// The `before` event runs immediately before a `then()` or `catch()` handler</span> 965<span class="hljs-comment">// runs or an `await` resumes execution.</span> 966<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">promise</span>) { 967 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a promise is about to call a then handler'</span>, { promise }); 968} 969 970<span class="hljs-comment">// The `after` event runs immediately after a `then()` handler runs or when</span> 971<span class="hljs-comment">// an `await` begins after resuming from another.</span> 972<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">promise</span>) { 973 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a promise is done calling a then handler'</span>, { promise }); 974} 975 976<span class="hljs-comment">// Lifecycle hooks may be started and stopped individually</span> 977<span class="hljs-keyword">const</span> stopWatchingInits = promiseHooks.<span class="hljs-title function_">onInit</span>(init); 978<span class="hljs-keyword">const</span> stopWatchingSettleds = promiseHooks.<span class="hljs-title function_">onSettled</span>(settled); 979<span class="hljs-keyword">const</span> stopWatchingBefores = promiseHooks.<span class="hljs-title function_">onBefore</span>(before); 980<span class="hljs-keyword">const</span> stopWatchingAfters = promiseHooks.<span class="hljs-title function_">onAfter</span>(after); 981 982<span class="hljs-comment">// Or they may be started and stopped in groups</span> 983<span class="hljs-keyword">const</span> stopHookSet = promiseHooks.<span class="hljs-title function_">createHook</span>({ 984 init, 985 settled, 986 before, 987 after, 988}); 989 990<span class="hljs-comment">// To stop a hook, call the function returned at its creation.</span> 991<span class="hljs-title function_">stopWatchingInits</span>(); 992<span class="hljs-title function_">stopWatchingSettleds</span>(); 993<span class="hljs-title function_">stopWatchingBefores</span>(); 994<span class="hljs-title function_">stopWatchingAfters</span>(); 995<span class="hljs-title function_">stopHookSet</span>();</code> <button class="copy-button">copy</button></pre> 996<h4><code>promiseHooks.onInit(init)</code><span><a class="mark" href="#promisehooksoninitinit" id="promisehooksoninitinit">#</a></span><a aria-hidden="true" class="legacy" id="v8_promisehooks_oninit_init"></a></h4> 997<div class="api_metadata"> 998<span>Added in: v17.1.0, v16.14.0</span> 999</div> 1000<ul> 1001<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#initpromise-parent"><code>init</code> callback</a> to call when a promise is created.</li> 1002<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Call to stop the hook.</li> 1003</ul> 1004<p><strong>The <code>init</code> hook must be a plain function. Providing an async function will 1005throw as it would produce an infinite microtask loop.</strong></p> 1006 1007<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 1008 1009<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onInit</span>(<span class="hljs-function">(<span class="hljs-params">promise, parent</span>) =></span> {});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { promiseHooks } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1010 1011<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onInit</span>(<span class="hljs-function">(<span class="hljs-params">promise, parent</span>) =></span> {});</code><button class="copy-button">copy</button></pre> 1012<h4><code>promiseHooks.onSettled(settled)</code><span><a class="mark" href="#promisehooksonsettledsettled" id="promisehooksonsettledsettled">#</a></span><a aria-hidden="true" class="legacy" id="v8_promisehooks_onsettled_settled"></a></h4> 1013<div class="api_metadata"> 1014<span>Added in: v17.1.0, v16.14.0</span> 1015</div> 1016<ul> 1017<li><code>settled</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#settledpromise"><code>settled</code> callback</a> to call when a promise 1018is resolved or rejected.</li> 1019<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Call to stop the hook.</li> 1020</ul> 1021<p><strong>The <code>settled</code> hook must be a plain function. Providing an async function will 1022throw as it would produce an infinite microtask loop.</strong></p> 1023 1024<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 1025 1026<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onSettled</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { promiseHooks } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1027 1028<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onSettled</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><button class="copy-button">copy</button></pre> 1029<h4><code>promiseHooks.onBefore(before)</code><span><a class="mark" href="#promisehooksonbeforebefore" id="promisehooksonbeforebefore">#</a></span><a aria-hidden="true" class="legacy" id="v8_promisehooks_onbefore_before"></a></h4> 1030<div class="api_metadata"> 1031<span>Added in: v17.1.0, v16.14.0</span> 1032</div> 1033<ul> 1034<li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#beforepromise"><code>before</code> callback</a> to call before a promise 1035continuation executes.</li> 1036<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Call to stop the hook.</li> 1037</ul> 1038<p><strong>The <code>before</code> hook must be a plain function. Providing an async function will 1039throw as it would produce an infinite microtask loop.</strong></p> 1040 1041<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 1042 1043<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onBefore</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { promiseHooks } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1044 1045<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onBefore</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><button class="copy-button">copy</button></pre> 1046<h4><code>promiseHooks.onAfter(after)</code><span><a class="mark" href="#promisehooksonafterafter" id="promisehooksonafterafter">#</a></span><a aria-hidden="true" class="legacy" id="v8_promisehooks_onafter_after"></a></h4> 1047<div class="api_metadata"> 1048<span>Added in: v17.1.0, v16.14.0</span> 1049</div> 1050<ul> 1051<li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#afterpromise"><code>after</code> callback</a> to call after a promise 1052continuation executes.</li> 1053<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Call to stop the hook.</li> 1054</ul> 1055<p><strong>The <code>after</code> hook must be a plain function. Providing an async function will 1056throw as it would produce an infinite microtask loop.</strong></p> 1057 1058<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 1059 1060<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onAfter</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { promiseHooks } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1061 1062<span class="hljs-keyword">const</span> stop = promiseHooks.<span class="hljs-title function_">onAfter</span>(<span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> {});</code><button class="copy-button">copy</button></pre> 1063<h4><code>promiseHooks.createHook(callbacks)</code><span><a class="mark" href="#promisehookscreatehookcallbacks" id="promisehookscreatehookcallbacks">#</a></span><a aria-hidden="true" class="legacy" id="v8_promisehooks_createhook_callbacks"></a></h4> 1064<div class="api_metadata"> 1065<span>Added in: v17.1.0, v16.14.0</span> 1066</div> 1067<ul> 1068<li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The <a href="#hook-callbacks">Hook Callbacks</a> to register 1069<ul> 1070<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#initpromise-parent"><code>init</code> callback</a>.</li> 1071<li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#beforepromise"><code>before</code> callback</a>.</li> 1072<li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#afterpromise"><code>after</code> callback</a>.</li> 1073<li><code>settled</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#settledpromise"><code>settled</code> callback</a>.</li> 1074</ul> 1075</li> 1076<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Used for disabling hooks</li> 1077</ul> 1078<p><strong>The hook callbacks must be plain functions. Providing async functions will 1079throw as it would produce an infinite microtask loop.</strong></p> 1080<p>Registers functions to be called for different lifetime events of each promise.</p> 1081<p>The callbacks <code>init()</code>/<code>before()</code>/<code>after()</code>/<code>settled()</code> are called for the 1082respective events during a promise's lifetime.</p> 1083<p>All callbacks are optional. For example, if only promise creation needs to 1084be tracked, then only the <code>init</code> callback needs to be passed. The 1085specifics of all functions that can be passed to <code>callbacks</code> is in the 1086<a href="#hook-callbacks">Hook Callbacks</a> section.</p> 1087 1088<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { promiseHooks } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:v8'</span>; 1089 1090<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({ 1091 <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {}, 1092});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { promiseHooks } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1093 1094<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({ 1095 <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {}, 1096});</code><button class="copy-button">copy</button></pre> 1097<h4>Hook callbacks<span><a class="mark" href="#hook-callbacks" id="hook-callbacks">#</a></span><a aria-hidden="true" class="legacy" id="v8_hook_callbacks"></a></h4> 1098<p>Key events in the lifetime of a promise have been categorized into four areas: 1099creation of a promise, before/after a continuation handler is called or around 1100an await, and when the promise resolves or rejects.</p> 1101<p>While these hooks are similar to those of <a href="async_hooks.html"><code>async_hooks</code></a> they lack a 1102<code>destroy</code> hook. Other types of async resources typically represent sockets or 1103file descriptors which have a distinct "closed" state to express the <code>destroy</code> 1104lifecycle event while promises remain usable for as long as code can still 1105reach them. Garbage collection tracking is used to make promises fit into the 1106<code>async_hooks</code> event model, however this tracking is very expensive and they may 1107not necessarily ever even be garbage collected.</p> 1108<p>Because promises are asynchronous resources whose lifecycle is tracked 1109via the promise hooks mechanism, the <code>init()</code>, <code>before()</code>, <code>after()</code>, and 1110<code>settled()</code> callbacks <em>must not</em> be async functions as they create more 1111promises which would produce an infinite loop.</p> 1112<p>While this API is used to feed promise events into <a href="async_hooks.html"><code>async_hooks</code></a>, the 1113ordering between the two is undefined. Both APIs are multi-tenant 1114and therefore could produce events in any order relative to each other.</p> 1115<h5><code>init(promise, parent)</code><span><a class="mark" href="#initpromise-parent" id="initpromise-parent">#</a></span><a aria-hidden="true" class="legacy" id="v8_init_promise_parent"></a></h5> 1116<ul> 1117<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> The promise being created.</li> 1118<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> The promise continued from, if applicable.</li> 1119</ul> 1120<p>Called when a promise is constructed. This <em>does not</em> mean that corresponding 1121<code>before</code>/<code>after</code> events will occur, only that the possibility exists. This will 1122happen if a promise is created without ever getting a continuation.</p> 1123<h5><code>before(promise)</code><span><a class="mark" href="#beforepromise" id="beforepromise">#</a></span><a aria-hidden="true" class="legacy" id="v8_before_promise"></a></h5> 1124<ul> 1125<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1126</ul> 1127<p>Called before a promise continuation executes. This can be in the form of 1128<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handlers or an <code>await</code> resuming.</p> 1129<p>The <code>before</code> callback will be called 0 to N times. The <code>before</code> callback 1130will typically be called 0 times if no continuation was ever made for the 1131promise. The <code>before</code> callback may be called many times in the case where 1132many continuations have been made from the same promise.</p> 1133<h5><code>after(promise)</code><span><a class="mark" href="#afterpromise" id="afterpromise">#</a></span><a aria-hidden="true" class="legacy" id="v8_after_promise"></a></h5> 1134<ul> 1135<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1136</ul> 1137<p>Called immediately after a promise continuation executes. This may be after a 1138<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handler or before an <code>await</code> after another 1139<code>await</code>.</p> 1140<h5><code>settled(promise)</code><span><a class="mark" href="#settledpromise" id="settledpromise">#</a></span><a aria-hidden="true" class="legacy" id="v8_settled_promise"></a></h5> 1141<ul> 1142<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1143</ul> 1144<p>Called when the promise receives a resolution or rejection value. This may 1145occur synchronously in the case of <code>Promise.resolve()</code> or <code>Promise.reject()</code>.</p> 1146</section><section><h3>Startup Snapshot API<span><a class="mark" href="#startup-snapshot-api" id="startup-snapshot-api">#</a></span><a aria-hidden="true" class="legacy" id="v8_startup_snapshot_api"></a></h3> 1147<div class="api_metadata"> 1148<span>Added in: v18.6.0</span> 1149</div> 1150<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1151<p>The <code>v8.startupSnapshot</code> interface can be used to add serialization and 1152deserialization hooks for custom startup snapshots.</p> 1153<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --snapshot-blob snapshot.blob --build-snapshot entry.js</span> 1154<span class="hljs-meta prompt_"># </span><span class="language-bash">This launches a process with the snapshot</span> 1155<span class="hljs-meta prompt_">$ </span><span class="language-bash">node --snapshot-blob snapshot.blob</span></code> <button class="copy-button">copy</button></pre> 1156<p>In the example above, <code>entry.js</code> can use methods from the <code>v8.startupSnapshot</code> 1157interface to specify how to save information for custom objects in the snapshot 1158during serialization and how the information can be used to synchronize these 1159objects during deserialization of the snapshot. For example, if the <code>entry.js</code> 1160contains the following script:</p> 1161<pre><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>; 1162 1163<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1164<span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:zlib'</span>); 1165<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 1166<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1167 1168<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1169 1170<span class="hljs-keyword">class</span> <span class="hljs-title class_">BookShelf</span> { 1171 storage = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(); 1172 1173 <span class="hljs-comment">// Reading a series of files from directory and store them into storage.</span> 1174 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">directory, books</span>) { 1175 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> book <span class="hljs-keyword">of</span> books) { 1176 <span class="hljs-variable language_">this</span>.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, fs.<span class="hljs-title function_">readFileSync</span>(path.<span class="hljs-title function_">join</span>(directory, book))); 1177 } 1178 } 1179 1180 <span class="hljs-keyword">static</span> <span class="hljs-title function_">compressAll</span>(<span class="hljs-params">shelf</span>) { 1181 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [ book, content ] <span class="hljs-keyword">of</span> shelf.<span class="hljs-property">storage</span>) { 1182 shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gzipSync</span>(content)); 1183 } 1184 } 1185 1186 <span class="hljs-keyword">static</span> <span class="hljs-title function_">decompressAll</span>(<span class="hljs-params">shelf</span>) { 1187 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [ book, content ] <span class="hljs-keyword">of</span> shelf.<span class="hljs-property">storage</span>) { 1188 shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gunzipSync</span>(content)); 1189 } 1190 } 1191} 1192 1193<span class="hljs-comment">// __dirname here is where the snapshot script is placed</span> 1194<span class="hljs-comment">// during snapshot building time.</span> 1195<span class="hljs-keyword">const</span> shelf = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BookShelf</span>(__dirname, [ 1196 <span class="hljs-string">'book1.en_US.txt'</span>, 1197 <span class="hljs-string">'book1.es_ES.txt'</span>, 1198 <span class="hljs-string">'book2.zh_CN.txt'</span>, 1199]); 1200 1201<span class="hljs-title function_">assert</span>(v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">isBuildingSnapshot</span>()); 1202<span class="hljs-comment">// On snapshot serialization, compress the books to reduce size.</span> 1203v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">addSerializeCallback</span>(<span class="hljs-title class_">BookShelf</span>.<span class="hljs-property">compressAll</span>, shelf); 1204<span class="hljs-comment">// On snapshot deserialization, decompress the books.</span> 1205v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">addDeserializeCallback</span>(<span class="hljs-title class_">BookShelf</span>.<span class="hljs-property">decompressAll</span>, shelf); 1206v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">setDeserializeMainFunction</span>(<span class="hljs-function">(<span class="hljs-params">shelf</span>) =></span> { 1207 <span class="hljs-comment">// process.env and process.argv are refreshed during snapshot</span> 1208 <span class="hljs-comment">// deserialization.</span> 1209 <span class="hljs-keyword">const</span> lang = process.<span class="hljs-property">env</span>.<span class="hljs-property">BOOK_LANG</span> || <span class="hljs-string">'en_US'</span>; 1210 <span class="hljs-keyword">const</span> book = process.<span class="hljs-property">argv</span>[<span class="hljs-number">1</span>]; 1211 <span class="hljs-keyword">const</span> name = <span class="hljs-string">`<span class="hljs-subst">${book}</span>.<span class="hljs-subst">${lang}</span>.txt`</span>; 1212 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">get</span>(name)); 1213}, shelf);</code> <button class="copy-button">copy</button></pre> 1214<p>The resulted binary will get print the data deserialized from the snapshot 1215during start up, using the refreshed <code>process.env</code> and <code>process.argv</code> of 1216the launched process:</p> 1217<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1</span> 1218<span class="hljs-meta prompt_"># </span><span class="language-bash">Prints content of book1.es_ES.txt deserialized from the snapshot.</span></code> <button class="copy-button">copy</button></pre> 1219<p>Currently the application deserialized from a user-land snapshot cannot 1220be snapshotted again, so these APIs are only available to applications 1221that are not deserialized from a user-land snapshot.</p> 1222<h4><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code><span><a class="mark" href="#v8startupsnapshotaddserializecallbackcallback-data" id="v8startupsnapshotaddserializecallbackcallback-data">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_startupsnapshot_addserializecallback_callback_data"></a></h4> 1223<div class="api_metadata"> 1224<span>Added in: v18.6.0</span> 1225</div> 1226<ul> 1227<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback to be invoked before serialization.</li> 1228<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional data that will be passed to the <code>callback</code> when it 1229gets called.</li> 1230</ul> 1231<p>Add a callback that will be called when the Node.js instance is about to 1232get serialized into a snapshot and exit. This can be used to release 1233resources that should not or cannot be serialized or to convert user data 1234into a form more suitable for serialization.</p> 1235<h4><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code><span><a class="mark" href="#v8startupsnapshotadddeserializecallbackcallback-data" id="v8startupsnapshotadddeserializecallbackcallback-data">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_startupsnapshot_adddeserializecallback_callback_data"></a></h4> 1236<div class="api_metadata"> 1237<span>Added in: v18.6.0</span> 1238</div> 1239<ul> 1240<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback to be invoked after the snapshot is 1241deserialized.</li> 1242<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional data that will be passed to the <code>callback</code> when it 1243gets called.</li> 1244</ul> 1245<p>Add a callback that will be called when the Node.js instance is deserialized 1246from a snapshot. The <code>callback</code> and the <code>data</code> (if provided) will be 1247serialized into the snapshot, they can be used to re-initialize the state 1248of the application or to re-acquire resources that the application needs 1249when the application is restarted from the snapshot.</p> 1250<h4><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code><span><a class="mark" href="#v8startupsnapshotsetdeserializemainfunctioncallback-data" id="v8startupsnapshotsetdeserializemainfunctioncallback-data">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_startupsnapshot_setdeserializemainfunction_callback_data"></a></h4> 1251<div class="api_metadata"> 1252<span>Added in: v18.6.0</span> 1253</div> 1254<ul> 1255<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Callback to be invoked as the entry point after the 1256snapshot is deserialized.</li> 1257<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional data that will be passed to the <code>callback</code> when it 1258gets called.</li> 1259</ul> 1260<p>This sets the entry point of the Node.js application when it is deserialized 1261from a snapshot. This can be called only once in the snapshot building 1262script. If called, the deserialized application no longer needs an additional 1263entry point script to start up and will simply invoke the callback along with 1264the deserialized data (if provided), otherwise an entry point script still 1265needs to be provided to the deserialized application.</p> 1266<h4><code>v8.startupSnapshot.isBuildingSnapshot()</code><span><a class="mark" href="#v8startupsnapshotisbuildingsnapshot" id="v8startupsnapshotisbuildingsnapshot">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8_startupsnapshot_isbuildingsnapshot"></a></h4> 1267<div class="api_metadata"> 1268<span>Added in: v18.6.0</span> 1269</div> 1270<ul> 1271<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1272</ul> 1273<p>Returns true if the Node.js instance is run to build a snapshot.</p> 1274</section><section><h3>Class: <code>v8.GCProfiler</code><span><a class="mark" href="#class-v8gcprofiler" id="class-v8gcprofiler">#</a></span><a aria-hidden="true" class="legacy" id="v8_class_v8_gcprofiler"></a></h3> 1275<div class="api_metadata"> 1276<span>Added in: v18.15.0</span> 1277</div> 1278<p>This API collects GC data in current thread.</p> 1279<h4><code>new v8.GCProfiler()</code><span><a class="mark" href="#new-v8gcprofiler" id="new-v8gcprofiler">#</a></span><a aria-hidden="true" class="legacy" id="v8_new_v8_gcprofiler"></a></h4> 1280<div class="api_metadata"> 1281<span>Added in: v18.15.0</span> 1282</div> 1283<p>Create a new instance of the <code>v8.GCProfiler</code> class.</p> 1284<h4><code>profiler.start()</code><span><a class="mark" href="#profilerstart" id="profilerstart">#</a></span><a aria-hidden="true" class="legacy" id="v8_profiler_start"></a></h4> 1285<div class="api_metadata"> 1286<span>Added in: v18.15.0</span> 1287</div> 1288<p>Start collecting GC data.</p> 1289<h4><code>profiler.stop()</code><span><a class="mark" href="#profilerstop" id="profilerstop">#</a></span><a aria-hidden="true" class="legacy" id="v8_profiler_stop"></a></h4> 1290<div class="api_metadata"> 1291<span>Added in: v18.15.0</span> 1292</div> 1293<p>Stop collecting GC data and return an object.The content of object 1294is as follows.</p> 1295<pre><code class="language-json"><span class="hljs-punctuation">{</span> 1296 <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> 1297 <span class="hljs-attr">"startTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059033862</span><span class="hljs-punctuation">,</span> 1298 <span class="hljs-attr">"statistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1299 <span class="hljs-punctuation">{</span> 1300 <span class="hljs-attr">"gcType"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Scavenge"</span><span class="hljs-punctuation">,</span> 1301 <span class="hljs-attr">"beforeGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1302 <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1303 <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5005312</span><span class="hljs-punctuation">,</span> 1304 <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span> 1305 <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5226496</span><span class="hljs-punctuation">,</span> 1306 <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341325216</span><span class="hljs-punctuation">,</span> 1307 <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span> 1308 <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span> 1309 <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4883840</span><span class="hljs-punctuation">,</span> 1310 <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span> 1311 <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span> 1312 <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span> 1313 <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span> 1314 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1315 <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1316 <span class="hljs-punctuation">{</span> 1317 <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span> 1318 <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1319 <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1320 <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1321 <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span> 1322 <span class="hljs-punctuation">}</span> 1323 <span class="hljs-punctuation">]</span> 1324 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1325 <span class="hljs-attr">"cost"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1574.14</span><span class="hljs-punctuation">,</span> 1326 <span class="hljs-attr">"afterGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1327 <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1328 <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">6053888</span><span class="hljs-punctuation">,</span> 1329 <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span> 1330 <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5500928</span><span class="hljs-punctuation">,</span> 1331 <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341101384</span><span class="hljs-punctuation">,</span> 1332 <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span> 1333 <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span> 1334 <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4059096</span><span class="hljs-punctuation">,</span> 1335 <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span> 1336 <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span> 1337 <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span> 1338 <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span> 1339 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1340 <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1341 <span class="hljs-punctuation">{</span> 1342 <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span> 1343 <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1344 <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1345 <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1346 <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span> 1347 <span class="hljs-punctuation">}</span> 1348 <span class="hljs-punctuation">]</span> 1349 <span class="hljs-punctuation">}</span> 1350 <span class="hljs-punctuation">}</span> 1351 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> 1352 <span class="hljs-attr">"endTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059036865</span> 1353<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 1354<p>Here's an example.</p> 1355<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">GCProfiler</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'v8'</span>); 1356<span class="hljs-keyword">const</span> profiler = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GCProfiler</span>(); 1357profiler.<span class="hljs-title function_">start</span>(); 1358<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1359 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(profiler.<span class="hljs-title function_">stop</span>()); 1360}, <span class="hljs-number">1000</span>);</code> <button class="copy-button">copy</button></pre></section> 1361 <!-- API END --> 1362 </div> 1363 </div> 1364 </div> 1365</body> 1366</html> 1367