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.20.1"> 7 <title>V8 | Node.js v18.20.1 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.20.1 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.20.1</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-v21.x/api/v8.html">21.x</a></li> 303<li><a href="https://nodejs.org/docs/latest-v20.x/api/v8.html">20.x <b>LTS</b></a></li> 304<li><a href="https://nodejs.org/docs/latest-v19.x/api/v8.html">19.x</a></li> 305<li><a href="https://nodejs.org/docs/latest-v18.x/api/v8.html">18.x <b>LTS</b></a></li> 306<li><a href="https://nodejs.org/docs/latest-v17.x/api/v8.html">17.x</a></li> 307<li><a href="https://nodejs.org/docs/latest-v16.x/api/v8.html">16.x</a></li> 308<li><a href="https://nodejs.org/docs/latest-v15.x/api/v8.html">15.x</a></li> 309<li><a href="https://nodejs.org/docs/latest-v14.x/api/v8.html">14.x</a></li> 310<li><a href="https://nodejs.org/docs/latest-v13.x/api/v8.html">13.x</a></li> 311<li><a href="https://nodejs.org/docs/latest-v12.x/api/v8.html">12.x</a></li> 312<li><a href="https://nodejs.org/docs/latest-v11.x/api/v8.html">11.x</a></li> 313<li><a href="https://nodejs.org/docs/latest-v10.x/api/v8.html">10.x</a></li> 314<li><a href="https://nodejs.org/docs/latest-v9.x/api/v8.html">9.x</a></li> 315<li><a href="https://nodejs.org/docs/latest-v8.x/api/v8.html">8.x</a></li> 316<li><a href="https://nodejs.org/docs/latest-v7.x/api/v8.html">7.x</a></li> 317<li><a href="https://nodejs.org/docs/latest-v6.x/api/v8.html">6.x</a></li> 318<li><a href="https://nodejs.org/docs/latest-v5.x/api/v8.html">5.x</a></li> 319<li><a href="https://nodejs.org/docs/latest-v4.x/api/v8.html">4.x</a></li></ol></div> 320 </li> 321 322 <li class="picker-header"> 323 <a href="#"> 324 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 325 Options 326 </a> 327 328 <div class="picker"> 329 <ul> 330 <li> 331 <a href="all.html">View on single page</a> 332 </li> 333 <li> 334 <a href="v8.json">View as JSON</a> 335 </li> 336 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/v8.md">Edit on GitHub</a></li> 337 </ul> 338 </div> 339 </li> 340 </ul> 341 </div> 342 <hr> 343 </header> 344 345 <details id="toc" open><summary>Table of contents</summary><ul> 346<li><a href="#v8">V8</a> 347<ul> 348<li><a href="#v8cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li> 349<li><a href="#v8getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li> 350<li><a href="#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li> 351<li><a href="#v8getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li> 352<li><a href="#v8getheapstatistics"><code>v8.getHeapStatistics()</code></a></li> 353<li><a href="#v8setflagsfromstringflags"><code>v8.setFlagsFromString(flags)</code></a></li> 354<li><a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a></li> 355<li><a href="#v8takecoverage"><code>v8.takeCoverage()</code></a></li> 356<li><a href="#v8writeheapsnapshotfilename"><code>v8.writeHeapSnapshot([filename])</code></a></li> 357<li><span class="stability_1"><a href="#v8setheapsnapshotnearheaplimitlimit"><code>v8.setHeapSnapshotNearHeapLimit(limit)</code></a></span></li> 358<li><a href="#serialization-api">Serialization API</a> 359<ul> 360<li><a href="#v8serializevalue"><code>v8.serialize(value)</code></a></li> 361<li><a href="#v8deserializebuffer"><code>v8.deserialize(buffer)</code></a></li> 362<li><a href="#class-v8serializer">Class: <code>v8.Serializer</code></a> 363<ul> 364<li><a href="#new-serializer"><code>new Serializer()</code></a></li> 365<li><a href="#serializerwriteheader"><code>serializer.writeHeader()</code></a></li> 366<li><a href="#serializerwritevaluevalue"><code>serializer.writeValue(value)</code></a></li> 367<li><a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a></li> 368<li><a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 369<li><a href="#serializerwriteuint32value"><code>serializer.writeUint32(value)</code></a></li> 370<li><a href="#serializerwriteuint64hi-lo"><code>serializer.writeUint64(hi, lo)</code></a></li> 371<li><a href="#serializerwritedoublevalue"><code>serializer.writeDouble(value)</code></a></li> 372<li><a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes(buffer)</code></a></li> 373<li><a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject(object)</code></a></li> 374<li><a href="#serializer_getdatacloneerrormessage"><code>serializer._getDataCloneError(message)</code></a></li> 375<li><a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li> 376<li><a href="#serializer_settreatarraybufferviewsashostobjectsflag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li> 377</ul> 378</li> 379<li><a href="#class-v8deserializer">Class: <code>v8.Deserializer</code></a> 380<ul> 381<li><a href="#new-deserializerbuffer"><code>new Deserializer(buffer)</code></a></li> 382<li><a href="#deserializerreadheader"><code>deserializer.readHeader()</code></a></li> 383<li><a href="#deserializerreadvalue"><code>deserializer.readValue()</code></a></li> 384<li><a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li> 385<li><a href="#deserializergetwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li> 386<li><a href="#deserializerreaduint32"><code>deserializer.readUint32()</code></a></li> 387<li><a href="#deserializerreaduint64"><code>deserializer.readUint64()</code></a></li> 388<li><a href="#deserializerreaddouble"><code>deserializer.readDouble()</code></a></li> 389<li><a href="#deserializerreadrawbyteslength"><code>deserializer.readRawBytes(length)</code></a></li> 390<li><a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li> 391</ul> 392</li> 393<li><a href="#class-v8defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li> 394<li><a href="#class-v8defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li> 395</ul> 396</li> 397<li><a href="#promise-hooks">Promise hooks</a> 398<ul> 399<li><a href="#promisehooksoninitinit"><code>promiseHooks.onInit(init)</code></a></li> 400<li><a href="#promisehooksonsettledsettled"><code>promiseHooks.onSettled(settled)</code></a></li> 401<li><a href="#promisehooksonbeforebefore"><code>promiseHooks.onBefore(before)</code></a></li> 402<li><a href="#promisehooksonafterafter"><code>promiseHooks.onAfter(after)</code></a></li> 403<li><a href="#promisehookscreatehookcallbacks"><code>promiseHooks.createHook(callbacks)</code></a></li> 404<li><a href="#hook-callbacks">Hook callbacks</a> 405<ul> 406<li><a href="#initpromise-parent"><code>init(promise, parent)</code></a></li> 407<li><a href="#beforepromise"><code>before(promise)</code></a></li> 408<li><a href="#afterpromise"><code>after(promise)</code></a></li> 409<li><a href="#settledpromise"><code>settled(promise)</code></a></li> 410</ul> 411</li> 412</ul> 413</li> 414<li><span class="stability_1"><a href="#startup-snapshot-api">Startup Snapshot API</a></span> 415<ul> 416<li><a href="#v8startupsnapshotaddserializecallbackcallback-data"><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code></a></li> 417<li><a href="#v8startupsnapshotadddeserializecallbackcallback-data"><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code></a></li> 418<li><a href="#v8startupsnapshotsetdeserializemainfunctioncallback-data"><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code></a></li> 419<li><a href="#v8startupsnapshotisbuildingsnapshot"><code>v8.startupSnapshot.isBuildingSnapshot()</code></a></li> 420</ul> 421</li> 422<li><a href="#class-v8gcprofiler">Class: <code>v8.GCProfiler</code></a> 423<ul> 424<li><a href="#new-v8gcprofiler"><code>new v8.GCProfiler()</code></a></li> 425<li><a href="#profilerstart"><code>profiler.start()</code></a></li> 426<li><a href="#profilerstop"><code>profiler.stop()</code></a></li> 427</ul> 428</li> 429</ul> 430</li> 431</ul></details> 432 433 <div id="apicontent"> 434 <h2>V8<span><a class="mark" href="#v8" id="v8">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8"></a></h2> 435 436<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/v8.js">lib/v8.js</a></p> 437<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> 438built into the Node.js binary. It can be accessed using:</p> 439<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> 440<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> 441<div class="api_metadata"> 442<span>Added in: v8.0.0</span> 443</div> 444<ul> 445<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 446</ul> 447<p>Returns an integer representing a version tag derived from the V8 version, 448command-line flags, and detected CPU features. This is useful for determining 449whether a <a href="vm.html#new-vmscriptcode-options"><code>vm.Script</code></a> <code>cachedData</code> buffer is compatible with this instance 450of V8.</p> 451<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> 452<span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V8</span> 453<span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the value</span> 454<span class="hljs-comment">// does indeed update when flags are toggled.</span> 455v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_natives_syntax'</span>); 456<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> 457</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> 458<div class="api_metadata"> 459<span>Added in: v12.8.0</span> 460</div> 461<ul> 462<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 463</ul> 464<p>Get statistics about code and its metadata in the heap, see V8 465<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 466following properties:</p> 467<ul> 468<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> 469<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> 470<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> 471<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> 472</ul> 473<!-- eslint-skip --> 474<pre><code class="language-js">{ 475 <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>, 476 <span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>, 477 <span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</span>, 478 <span class="hljs-attr">cpu_profiler_metadata_size</span>: <span class="hljs-number">0</span>, 479}</code> <button class="copy-button">copy</button></pre> 480</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> 481<div class="api_metadata"> 482<span>Added in: v11.13.0</span> 483</div> 484<ul> 485<li>Returns: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> A Readable Stream containing the V8 heap snapshot</li> 486</ul> 487<p>Generates a snapshot of the current V8 heap and returns a Readable 488Stream that may be used to read the JSON serialized representation. 489This JSON stream format is intended to be used with tools such as 490Chrome DevTools. The JSON schema is undocumented and specific to the 491V8 engine. Therefore, the schema may change from one version of V8 to the next.</p> 492<p>Creating a heap snapshot requires memory about twice the size of the heap at 493the time the snapshot is created. This results in the risk of OOM killers 494terminating the process.</p> 495<p>Generating a snapshot is a synchronous operation which blocks the event loop 496for a duration depending on the heap size.</p> 497<pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</span> 498<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 499<span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnapshot</span>(); 500stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code> <button class="copy-button">copy</button></pre> 501</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> 502<div class="api_metadata"> 503<details class="changelog"><summary>History</summary> 504<table> 505<tbody><tr><th>Version</th><th>Changes</th></tr> 506<tr><td>v7.5.0</td> 507<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr> 508<tr><td>v6.0.0</td> 509<td><p><span>Added in: v6.0.0</span></p></td></tr> 510</tbody></table> 511</details> 512</div> 513<ul> 514<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a></li> 515</ul> 516<p>Returns statistics about the V8 heap spaces, i.e. the segments which make up 517the V8 heap. Neither the ordering of heap spaces, nor the availability of a 518heap space can be guaranteed as the statistics are provided via the V8 519<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 520next.</p> 521<p>The value returned is an array of objects containing the following properties:</p> 522<ul> 523<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> 524<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> 525<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> 526<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> 527<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> 528</ul> 529<pre><code class="language-json"><span class="hljs-punctuation">[</span> 530 <span class="hljs-punctuation">{</span> 531 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"new_space"</span><span class="hljs-punctuation">,</span> 532 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span><span class="hljs-punctuation">,</span> 533 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">951112</span><span class="hljs-punctuation">,</span> 534 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">80824</span><span class="hljs-punctuation">,</span> 535 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span> 536 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 537 <span class="hljs-punctuation">{</span> 538 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"old_space"</span><span class="hljs-punctuation">,</span> 539 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span><span class="hljs-punctuation">,</span> 540 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2493792</span><span class="hljs-punctuation">,</span> 541 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 542 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span> 543 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 544 <span class="hljs-punctuation">{</span> 545 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"code_space"</span><span class="hljs-punctuation">,</span> 546 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span><span class="hljs-punctuation">,</span> 547 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">644256</span><span class="hljs-punctuation">,</span> 548 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">960</span><span class="hljs-punctuation">,</span> 549 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span> 550 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 551 <span class="hljs-punctuation">{</span> 552 <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"map_space"</span><span class="hljs-punctuation">,</span> 553 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span><span class="hljs-punctuation">,</span> 554 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">201608</span><span class="hljs-punctuation">,</span> 555 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 556 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span> 557 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 558 <span class="hljs-punctuation">{</span> 559 <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> 560 <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 561 <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 562 <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1490980608</span><span class="hljs-punctuation">,</span> 563 <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span> 564 <span class="hljs-punctuation">}</span> 565<span class="hljs-punctuation">]</span></code> <button class="copy-button">copy</button></pre> 566</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> 567<div class="api_metadata"> 568<details class="changelog"><summary>History</summary> 569<table> 570<tbody><tr><th>Version</th><th>Changes</th></tr> 571<tr><td>v7.5.0</td> 572<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr> 573<tr><td>v7.2.0</td> 574<td><p>Added <code>malloced_memory</code>, <code>peak_malloced_memory</code>, and <code>does_zap_garbage</code>.</p></td></tr> 575<tr><td>v1.0.0</td> 576<td><p><span>Added in: v1.0.0</span></p></td></tr> 577</tbody></table> 578</details> 579</div> 580<ul> 581<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 582</ul> 583<p>Returns an object with the following properties:</p> 584<ul> 585<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> 586<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> 587<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> 588<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> 589<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> 590<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> 591<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> 592<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> 593<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> 594<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> 595<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> 596<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> 597<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> 598<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> 599</ul> 600<p><code>does_zap_garbage</code> is a 0/1 boolean, which signifies whether the 601<code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap 602garbage with a bit pattern. The RSS footprint (resident set size) gets bigger 603because it continuously touches all heap pages and that makes them less likely 604to get swapped out by the operating system.</p> 605<p><code>number_of_native_contexts</code> The value of native_context is the number of the 606top-level contexts currently active. Increase of this number over time indicates 607a memory leak.</p> 608<p><code>number_of_detached_contexts</code> The value of detached_context is the number 609of contexts that were detached and not yet garbage collected. This number 610being non-zero indicates a potential memory leak.</p> 611<p><code>total_global_handles_size</code> The value of total_global_handles_size is the 612total memory size of V8 global handles.</p> 613<p><code>used_global_handles_size</code> The value of used_global_handles_size is the 614used memory size of V8 global handles.</p> 615<p><code>external_memory</code> The value of external_memory is the memory size of array 616buffers and external strings.</p> 617<!-- eslint-skip --> 618<pre><code class="language-js">{ 619 <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>, 620 <span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span>, 621 <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>, 622 <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>, 623 <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>, 624 <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>, 625 <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>, 626 <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>, 627 <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>, 628 <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>, 629 <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>, 630 <span class="hljs-attr">total_global_handles_size</span>: <span class="hljs-number">8192</span>, 631 <span class="hljs-attr">used_global_handles_size</span>: <span class="hljs-number">3296</span>, 632 <span class="hljs-attr">external_memory</span>: <span class="hljs-number">318824</span> 633}</code> <button class="copy-button">copy</button></pre> 634</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> 635<div class="api_metadata"> 636<span>Added in: v1.0.0</span> 637</div> 638<ul> 639<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 640</ul> 641<p>The <code>v8.setFlagsFromString()</code> method can be used to programmatically set 642V8 command-line flags. This method should be used with care. Changing settings 643after the VM has started may result in unpredictable behavior, including 644crashes and data loss; or it may simply do nothing.</p> 645<p>The V8 options available for a version of Node.js may be determined by running 646<code>node --v8-options</code>.</p> 647<p>Usage:</p> 648<pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minute.</span> 649<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 650v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_gc'</span>); 651<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> 652</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> 653<div class="api_metadata"> 654<span>Added in: v15.1.0, v14.18.0, v12.22.0</span> 655</div> 656<p>The <code>v8.stopCoverage()</code> method allows the user to stop the coverage collection 657started by <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>, so that V8 can release the execution count 658records and optimize code. This can be used in conjunction with 659<a href="#v8takecoverage"><code>v8.takeCoverage()</code></a> if the user wants to collect the coverage on demand.</p> 660</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> 661<div class="api_metadata"> 662<span>Added in: v15.1.0, v14.18.0, v12.22.0</span> 663</div> 664<p>The <code>v8.takeCoverage()</code> method allows the user to write the coverage started by 665<a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> to disk on demand. This method can be invoked multiple 666times during the lifetime of the process. Each time the execution counter will 667be reset and a new coverage report will be written to the directory specified 668by <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>.</p> 669<p>When the process is about to exit, one last coverage will still be written to 670disk unless <a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a> is invoked before the process exits.</p> 671</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> 672<div class="api_metadata"> 673<details class="changelog"><summary>History</summary> 674<table> 675<tbody><tr><th>Version</th><th>Changes</th></tr> 676<tr><td>v18.0.0</td> 677<td><p>An exception will now be thrown if the file could not be written.</p></td></tr> 678<tr><td>v18.0.0</td> 679<td><p>Make the returned error codes consistent across all platforms.</p></td></tr> 680<tr><td>v11.13.0</td> 681<td><p><span>Added in: v11.13.0</span></p></td></tr> 682</tbody></table> 683</details> 684</div> 685<ul> 686<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 687saved. If not specified, a file name with the pattern 688<code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be 689generated, where <code>{pid}</code> will be the PID of the Node.js process, 690<code>{thread_id}</code> will be <code>0</code> when <code>writeHeapSnapshot()</code> is called from 691the main Node.js thread or the id of a worker thread.</li> 692<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> 693</ul> 694<p>Generates a snapshot of the current V8 heap and writes it to a JSON 695file. This file is intended to be used with tools such as Chrome 696DevTools. The JSON schema is undocumented and specific to the V8 697engine, and may change from one version of V8 to the next.</p> 698<p>A heap snapshot is specific to a single V8 isolate. When using 699<a href="worker_threads.html">worker threads</a>, a heap snapshot generated from the main thread will 700not contain any information about the workers, and vice versa.</p> 701<p>Creating a heap snapshot requires memory about twice the size of the heap at 702the time the snapshot is created. This results in the risk of OOM killers 703terminating the process.</p> 704<p>Generating a snapshot is a synchronous operation which blocks the event loop 705for a duration depending on the heap size.</p> 706<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>); 707<span class="hljs-keyword">const</span> { 708 <span class="hljs-title class_">Worker</span>, 709 isMainThread, 710 parentPort, 711} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 712 713<span class="hljs-keyword">if</span> (isMainThread) { 714 <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 715 716 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> { 717 <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>); 718 <span class="hljs-comment">// Now get a heapdump for the main thread.</span> 719 <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>); 720 }); 721 722 <span class="hljs-comment">// Tell the worker to create a heapdump.</span> 723 worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'</span>); 724} <span class="hljs-keyword">else</span> { 725 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> { 726 <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) { 727 <span class="hljs-comment">// Generate a heapdump for the worker</span> 728 <span class="hljs-comment">// and return the filename to the parent.</span> 729 parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">writeHeapSnapshot</span>()); 730 } 731 }); 732}</code> <button class="copy-button">copy</button></pre> 733</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> 734<div class="api_metadata"> 735<span>Added in: v18.10.0</span> 736</div> 737<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 738<ul> 739<li><code>limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 740</ul> 741<p>The API is a no-op if <code>--heapsnapshot-near-heap-limit</code> is already set from the 742command line or the API is called more than once. <code>limit</code> must be a positive 743integer. See <a href="cli.html#--heapsnapshot-near-heap-limitmax_count"><code>--heapsnapshot-near-heap-limit</code></a> for more information.</p> 744</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> 745<p>The serialization API provides means of serializing JavaScript values in a way 746that 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> 747<p>The format is backward-compatible (i.e. safe to store to disk). 748Equal JavaScript values may result in different serialized output.</p> 749<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> 750<div class="api_metadata"> 751<span>Added in: v8.0.0</span> 752</div> 753<ul> 754<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 755<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 756</ul> 757<p>Uses a <a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a> to serialize <code>value</code> into a buffer.</p> 758<p><a href="errors.html#err_buffer_too_large"><code>ERR_BUFFER_TOO_LARGE</code></a> will be thrown when trying to 759serialize a huge object which requires buffer 760larger than <a href="buffer.html#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> 761<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> 762<div class="api_metadata"> 763<span>Added in: v8.0.0</span> 764</div> 765<ul> 766<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> 767</ul> 768<p>Uses a <a href="#class-v8defaultdeserializer"><code>DefaultDeserializer</code></a> with default options to read a JS value 769from a buffer.</p> 770<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> 771<div class="api_metadata"> 772<span>Added in: v8.0.0</span> 773</div> 774<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> 775<p>Creates a new <code>Serializer</code> object.</p> 776<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> 777<p>Writes out a header, which includes the serialization format version.</p> 778<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> 779<ul> 780<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 781</ul> 782<p>Serializes a JavaScript value and adds the serialized representation to the 783internal buffer.</p> 784<p>This throws an error if <code>value</code> cannot be serialized.</p> 785<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> 786<ul> 787<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 788</ul> 789<p>Returns the stored internal buffer. This serializer should not be used once 790the buffer is released. Calling this method results in undefined behavior 791if a previous write has failed.</p> 792<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> 793<ul> 794<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> 795<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> 796</ul> 797<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band. 798Pass the corresponding <code>ArrayBuffer</code> in the deserializing context to 799<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p> 800<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> 801<ul> 802<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 803</ul> 804<p>Write a raw 32-bit unsigned integer. 805For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 806<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> 807<ul> 808<li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 809<li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 810</ul> 811<p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts. 812For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 813<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> 814<ul> 815<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 816</ul> 817<p>Write a JS <code>number</code> value. 818For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 819<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> 820<ul> 821<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> 822</ul> 823<p>Write raw bytes into the serializer's internal buffer. The deserializer 824will require a way to compute the length of the buffer. 825For use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 826<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> 827<ul> 828<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 829</ul> 830<p>This method is called to write some kind of host object, i.e. an object created 831by native C++ bindings. If it is not possible to serialize <code>object</code>, a suitable 832exception should be thrown.</p> 833<p>This method is not present on the <code>Serializer</code> class itself but can be provided 834by subclasses.</p> 835<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> 836<ul> 837<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 838</ul> 839<p>This method is called to generate error objects that will be thrown when an 840object can not be cloned.</p> 841<p>This method defaults to the <a href="errors.html#class-error"><code>Error</code></a> constructor and can be overridden on 842subclasses.</p> 843<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> 844<ul> 845<li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a></li> 846</ul> 847<p>This method is called when the serializer is going to serialize a 848<code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for 849the object, using the same ID if this <code>SharedArrayBuffer</code> has already been 850serialized. When deserializing, this ID will be passed to 851<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p> 852<p>If the object cannot be serialized, an exception should be thrown.</p> 853<p>This method is not present on the <code>Serializer</code> class itself but can be provided 854by subclasses.</p> 855<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> 856<ul> 857<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> 858</ul> 859<p>Indicate whether to treat <code>TypedArray</code> and <code>DataView</code> objects as 860host objects, i.e. pass them to <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p> 861<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> 862<div class="api_metadata"> 863<span>Added in: v8.0.0</span> 864</div> 865<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> 866<ul> 867<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 868<a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a>.</li> 869</ul> 870<p>Creates a new <code>Deserializer</code> object.</p> 871<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> 872<p>Reads and validates a header (including the format version). 873May, for example, reject an invalid or unsupported wire format. In that case, 874an <code>Error</code> is thrown.</p> 875<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> 876<p>Deserializes a JavaScript value from the buffer and returns it.</p> 877<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> 878<ul> 879<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> 880<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> 881</ul> 882<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band. 883Pass the corresponding <code>ArrayBuffer</code> in the serializing context to 884<a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer()</code></a> (or return the <code>id</code> from 885<a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId()</code></a> in the case of <code>SharedArrayBuffer</code>s).</p> 886<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> 887<ul> 888<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 889</ul> 890<p>Reads the underlying wire format version. Likely mostly to be useful to 891legacy code reading old wire format versions. May not be called before 892<code>.readHeader()</code>.</p> 893<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> 894<ul> 895<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 896</ul> 897<p>Read a raw 32-bit unsigned integer and return it. 898For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 899<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> 900<ul> 901<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 902</ul> 903<p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code> 904with two 32-bit unsigned integer entries. 905For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 906<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> 907<ul> 908<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 909</ul> 910<p>Read a JS <code>number</code> value. 911For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 912<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> 913<ul> 914<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 915<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 916</ul> 917<p>Read raw bytes from the deserializer's internal buffer. The <code>length</code> parameter 918must correspond to the length of the buffer that was passed to 919<a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes()</code></a>. 920For use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p> 921<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> 922<p>This method is called to read some kind of host object, i.e. an object that is 923created by native C++ bindings. If it is not possible to deserialize the data, 924a suitable exception should be thrown.</p> 925<p>This method is not present on the <code>Deserializer</code> class itself but can be 926provided by subclasses.</p> 927<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> 928<div class="api_metadata"> 929<span>Added in: v8.0.0</span> 930</div> 931<p>A subclass of <a href="#class-v8serializer"><code>Serializer</code></a> that serializes <code>TypedArray</code> 932(in particular <a href="buffer.html"><code>Buffer</code></a>) and <code>DataView</code> objects as host objects, and only 933stores the part of their underlying <code>ArrayBuffer</code>s that they are referring to.</p> 934<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> 935<div class="api_metadata"> 936<span>Added in: v8.0.0</span> 937</div> 938<p>A subclass of <a href="#class-v8deserializer"><code>Deserializer</code></a> corresponding to the format written by 939<a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a>.</p> 940</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> 941<p>The <code>promiseHooks</code> interface can be used to track promise lifecycle events. 942To track <em>all</em> async activity, see <a href="async_hooks.html"><code>async_hooks</code></a> which internally uses this 943module to produce promise lifecycle events in addition to events for other 944async resources. For request context management, see <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.</p> 945<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>; 946 947<span class="hljs-comment">// There are four lifecycle events produced by promises:</span> 948 949<span class="hljs-comment">// The `init` event represents the creation of a promise. This could be a</span> 950<span class="hljs-comment">// direct creation such as with `new Promise(...)` or a continuation such</span> 951<span class="hljs-comment">// as `then()` or `catch()`. It also happens whenever an async function is</span> 952<span class="hljs-comment">// called or does an `await`. If a continuation promise is created, the</span> 953<span class="hljs-comment">// `parent` will be the promise it is a continuation from.</span> 954<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) { 955 <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 }); 956} 957 958<span class="hljs-comment">// The `settled` event happens when a promise receives a resolution or</span> 959<span class="hljs-comment">// rejection value. This may happen synchronously such as when using</span> 960<span class="hljs-comment">// `Promise.resolve()` on non-promise input.</span> 961<span class="hljs-keyword">function</span> <span class="hljs-title function_">settled</span>(<span class="hljs-params">promise</span>) { 962 <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 }); 963} 964 965<span class="hljs-comment">// The `before` event runs immediately before a `then()` or `catch()` handler</span> 966<span class="hljs-comment">// runs or an `await` resumes execution.</span> 967<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">promise</span>) { 968 <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 }); 969} 970 971<span class="hljs-comment">// The `after` event runs immediately after a `then()` handler runs or when</span> 972<span class="hljs-comment">// an `await` begins after resuming from another.</span> 973<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">promise</span>) { 974 <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 }); 975} 976 977<span class="hljs-comment">// Lifecycle hooks may be started and stopped individually</span> 978<span class="hljs-keyword">const</span> stopWatchingInits = promiseHooks.<span class="hljs-title function_">onInit</span>(init); 979<span class="hljs-keyword">const</span> stopWatchingSettleds = promiseHooks.<span class="hljs-title function_">onSettled</span>(settled); 980<span class="hljs-keyword">const</span> stopWatchingBefores = promiseHooks.<span class="hljs-title function_">onBefore</span>(before); 981<span class="hljs-keyword">const</span> stopWatchingAfters = promiseHooks.<span class="hljs-title function_">onAfter</span>(after); 982 983<span class="hljs-comment">// Or they may be started and stopped in groups</span> 984<span class="hljs-keyword">const</span> stopHookSet = promiseHooks.<span class="hljs-title function_">createHook</span>({ 985 init, 986 settled, 987 before, 988 after, 989}); 990 991<span class="hljs-comment">// To stop a hook, call the function returned at its creation.</span> 992<span class="hljs-title function_">stopWatchingInits</span>(); 993<span class="hljs-title function_">stopWatchingSettleds</span>(); 994<span class="hljs-title function_">stopWatchingBefores</span>(); 995<span class="hljs-title function_">stopWatchingAfters</span>(); 996<span class="hljs-title function_">stopHookSet</span>();</code> <button class="copy-button">copy</button></pre> 997<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> 998<div class="api_metadata"> 999<span>Added in: v17.1.0, v16.14.0</span> 1000</div> 1001<ul> 1002<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> 1003<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> 1004</ul> 1005<p><strong>The <code>init</code> hook must be a plain function. Providing an async function will 1006throw as it would produce an infinite microtask loop.</strong></p> 1007 1008<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>; 1009 1010<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>); 1011 1012<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> 1013<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> 1014<div class="api_metadata"> 1015<span>Added in: v17.1.0, v16.14.0</span> 1016</div> 1017<ul> 1018<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 1019is resolved or rejected.</li> 1020<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> 1021</ul> 1022<p><strong>The <code>settled</code> hook must be a plain function. Providing an async function will 1023throw as it would produce an infinite microtask loop.</strong></p> 1024 1025<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>; 1026 1027<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>); 1028 1029<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> 1030<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> 1031<div class="api_metadata"> 1032<span>Added in: v17.1.0, v16.14.0</span> 1033</div> 1034<ul> 1035<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 1036continuation executes.</li> 1037<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> 1038</ul> 1039<p><strong>The <code>before</code> hook must be a plain function. Providing an async function will 1040throw as it would produce an infinite microtask loop.</strong></p> 1041 1042<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>; 1043 1044<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>); 1045 1046<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> 1047<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> 1048<div class="api_metadata"> 1049<span>Added in: v17.1.0, v16.14.0</span> 1050</div> 1051<ul> 1052<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 1053continuation executes.</li> 1054<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> 1055</ul> 1056<p><strong>The <code>after</code> hook must be a plain function. Providing an async function will 1057throw as it would produce an infinite microtask loop.</strong></p> 1058 1059<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>; 1060 1061<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>); 1062 1063<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> 1064<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> 1065<div class="api_metadata"> 1066<span>Added in: v17.1.0, v16.14.0</span> 1067</div> 1068<ul> 1069<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 1070<ul> 1071<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> 1072<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> 1073<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> 1074<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> 1075</ul> 1076</li> 1077<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> 1078</ul> 1079<p><strong>The hook callbacks must be plain functions. Providing async functions will 1080throw as it would produce an infinite microtask loop.</strong></p> 1081<p>Registers functions to be called for different lifetime events of each promise.</p> 1082<p>The callbacks <code>init()</code>/<code>before()</code>/<code>after()</code>/<code>settled()</code> are called for the 1083respective events during a promise's lifetime.</p> 1084<p>All callbacks are optional. For example, if only promise creation needs to 1085be tracked, then only the <code>init</code> callback needs to be passed. The 1086specifics of all functions that can be passed to <code>callbacks</code> is in the 1087<a href="#hook-callbacks">Hook Callbacks</a> section.</p> 1088 1089<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>; 1090 1091<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({ 1092 <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {}, 1093});</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>); 1094 1095<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({ 1096 <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {}, 1097});</code><button class="copy-button">copy</button></pre> 1098<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> 1099<p>Key events in the lifetime of a promise have been categorized into four areas: 1100creation of a promise, before/after a continuation handler is called or around 1101an await, and when the promise resolves or rejects.</p> 1102<p>While these hooks are similar to those of <a href="async_hooks.html"><code>async_hooks</code></a> they lack a 1103<code>destroy</code> hook. Other types of async resources typically represent sockets or 1104file descriptors which have a distinct "closed" state to express the <code>destroy</code> 1105lifecycle event while promises remain usable for as long as code can still 1106reach them. Garbage collection tracking is used to make promises fit into the 1107<code>async_hooks</code> event model, however this tracking is very expensive and they may 1108not necessarily ever even be garbage collected.</p> 1109<p>Because promises are asynchronous resources whose lifecycle is tracked 1110via the promise hooks mechanism, the <code>init()</code>, <code>before()</code>, <code>after()</code>, and 1111<code>settled()</code> callbacks <em>must not</em> be async functions as they create more 1112promises which would produce an infinite loop.</p> 1113<p>While this API is used to feed promise events into <a href="async_hooks.html"><code>async_hooks</code></a>, the 1114ordering between the two is undefined. Both APIs are multi-tenant 1115and therefore could produce events in any order relative to each other.</p> 1116<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> 1117<ul> 1118<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> 1119<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> 1120</ul> 1121<p>Called when a promise is constructed. This <em>does not</em> mean that corresponding 1122<code>before</code>/<code>after</code> events will occur, only that the possibility exists. This will 1123happen if a promise is created without ever getting a continuation.</p> 1124<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> 1125<ul> 1126<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1127</ul> 1128<p>Called before a promise continuation executes. This can be in the form of 1129<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handlers or an <code>await</code> resuming.</p> 1130<p>The <code>before</code> callback will be called 0 to N times. The <code>before</code> callback 1131will typically be called 0 times if no continuation was ever made for the 1132promise. The <code>before</code> callback may be called many times in the case where 1133many continuations have been made from the same promise.</p> 1134<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> 1135<ul> 1136<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1137</ul> 1138<p>Called immediately after a promise continuation executes. This may be after a 1139<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handler or before an <code>await</code> after another 1140<code>await</code>.</p> 1141<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> 1142<ul> 1143<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1144</ul> 1145<p>Called when the promise receives a resolution or rejection value. This may 1146occur synchronously in the case of <code>Promise.resolve()</code> or <code>Promise.reject()</code>.</p> 1147</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> 1148<div class="api_metadata"> 1149<span>Added in: v18.6.0</span> 1150</div> 1151<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1152<p>The <code>v8.startupSnapshot</code> interface can be used to add serialization and 1153deserialization hooks for custom startup snapshots.</p> 1154<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> 1155<span class="hljs-meta prompt_"># </span><span class="language-bash">This launches a process with the snapshot</span> 1156<span class="hljs-meta prompt_">$ </span><span class="language-bash">node --snapshot-blob snapshot.blob</span></code> <button class="copy-button">copy</button></pre> 1157<p>In the example above, <code>entry.js</code> can use methods from the <code>v8.startupSnapshot</code> 1158interface to specify how to save information for custom objects in the snapshot 1159during serialization and how the information can be used to synchronize these 1160objects during deserialization of the snapshot. For example, if the <code>entry.js</code> 1161contains the following script:</p> 1162<pre><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>; 1163 1164<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1165<span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:zlib'</span>); 1166<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 1167<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1168 1169<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>); 1170 1171<span class="hljs-keyword">class</span> <span class="hljs-title class_">BookShelf</span> { 1172 storage = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(); 1173 1174 <span class="hljs-comment">// Reading a series of files from directory and store them into storage.</span> 1175 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">directory, books</span>) { 1176 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> book <span class="hljs-keyword">of</span> books) { 1177 <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))); 1178 } 1179 } 1180 1181 <span class="hljs-keyword">static</span> <span class="hljs-title function_">compressAll</span>(<span class="hljs-params">shelf</span>) { 1182 <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>) { 1183 shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gzipSync</span>(content)); 1184 } 1185 } 1186 1187 <span class="hljs-keyword">static</span> <span class="hljs-title function_">decompressAll</span>(<span class="hljs-params">shelf</span>) { 1188 <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>) { 1189 shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gunzipSync</span>(content)); 1190 } 1191 } 1192} 1193 1194<span class="hljs-comment">// __dirname here is where the snapshot script is placed</span> 1195<span class="hljs-comment">// during snapshot building time.</span> 1196<span class="hljs-keyword">const</span> shelf = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BookShelf</span>(__dirname, [ 1197 <span class="hljs-string">'book1.en_US.txt'</span>, 1198 <span class="hljs-string">'book1.es_ES.txt'</span>, 1199 <span class="hljs-string">'book2.zh_CN.txt'</span>, 1200]); 1201 1202<span class="hljs-title function_">assert</span>(v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">isBuildingSnapshot</span>()); 1203<span class="hljs-comment">// On snapshot serialization, compress the books to reduce size.</span> 1204v8.<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); 1205<span class="hljs-comment">// On snapshot deserialization, decompress the books.</span> 1206v8.<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); 1207v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">setDeserializeMainFunction</span>(<span class="hljs-function">(<span class="hljs-params">shelf</span>) =></span> { 1208 <span class="hljs-comment">// process.env and process.argv are refreshed during snapshot</span> 1209 <span class="hljs-comment">// deserialization.</span> 1210 <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>; 1211 <span class="hljs-keyword">const</span> book = process.<span class="hljs-property">argv</span>[<span class="hljs-number">1</span>]; 1212 <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>; 1213 <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)); 1214}, shelf);</code> <button class="copy-button">copy</button></pre> 1215<p>The resulted binary will get print the data deserialized from the snapshot 1216during start up, using the refreshed <code>process.env</code> and <code>process.argv</code> of 1217the launched process:</p> 1218<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> 1219<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> 1220<p>Currently the application deserialized from a user-land snapshot cannot 1221be snapshotted again, so these APIs are only available to applications 1222that are not deserialized from a user-land snapshot.</p> 1223<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> 1224<div class="api_metadata"> 1225<span>Added in: v18.6.0</span> 1226</div> 1227<ul> 1228<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> 1229<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 1230gets called.</li> 1231</ul> 1232<p>Add a callback that will be called when the Node.js instance is about to 1233get serialized into a snapshot and exit. This can be used to release 1234resources that should not or cannot be serialized or to convert user data 1235into a form more suitable for serialization.</p> 1236<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> 1237<div class="api_metadata"> 1238<span>Added in: v18.6.0</span> 1239</div> 1240<ul> 1241<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 1242deserialized.</li> 1243<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 1244gets called.</li> 1245</ul> 1246<p>Add a callback that will be called when the Node.js instance is deserialized 1247from a snapshot. The <code>callback</code> and the <code>data</code> (if provided) will be 1248serialized into the snapshot, they can be used to re-initialize the state 1249of the application or to re-acquire resources that the application needs 1250when the application is restarted from the snapshot.</p> 1251<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> 1252<div class="api_metadata"> 1253<span>Added in: v18.6.0</span> 1254</div> 1255<ul> 1256<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 1257snapshot is deserialized.</li> 1258<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 1259gets called.</li> 1260</ul> 1261<p>This sets the entry point of the Node.js application when it is deserialized 1262from a snapshot. This can be called only once in the snapshot building 1263script. If called, the deserialized application no longer needs an additional 1264entry point script to start up and will simply invoke the callback along with 1265the deserialized data (if provided), otherwise an entry point script still 1266needs to be provided to the deserialized application.</p> 1267<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> 1268<div class="api_metadata"> 1269<span>Added in: v18.6.0</span> 1270</div> 1271<ul> 1272<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1273</ul> 1274<p>Returns true if the Node.js instance is run to build a snapshot.</p> 1275</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> 1276<div class="api_metadata"> 1277<span>Added in: v18.15.0</span> 1278</div> 1279<p>This API collects GC data in current thread.</p> 1280<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> 1281<div class="api_metadata"> 1282<span>Added in: v18.15.0</span> 1283</div> 1284<p>Create a new instance of the <code>v8.GCProfiler</code> class.</p> 1285<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> 1286<div class="api_metadata"> 1287<span>Added in: v18.15.0</span> 1288</div> 1289<p>Start collecting GC data.</p> 1290<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> 1291<div class="api_metadata"> 1292<span>Added in: v18.15.0</span> 1293</div> 1294<p>Stop collecting GC data and return an object.The content of object 1295is as follows.</p> 1296<pre><code class="language-json"><span class="hljs-punctuation">{</span> 1297 <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> 1298 <span class="hljs-attr">"startTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059033862</span><span class="hljs-punctuation">,</span> 1299 <span class="hljs-attr">"statistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1300 <span class="hljs-punctuation">{</span> 1301 <span class="hljs-attr">"gcType"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Scavenge"</span><span class="hljs-punctuation">,</span> 1302 <span class="hljs-attr">"beforeGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1303 <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1304 <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5005312</span><span class="hljs-punctuation">,</span> 1305 <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span> 1306 <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5226496</span><span class="hljs-punctuation">,</span> 1307 <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341325216</span><span class="hljs-punctuation">,</span> 1308 <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span> 1309 <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span> 1310 <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4883840</span><span class="hljs-punctuation">,</span> 1311 <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span> 1312 <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span> 1313 <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span> 1314 <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span> 1315 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1316 <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1317 <span class="hljs-punctuation">{</span> 1318 <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span> 1319 <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1320 <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1321 <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1322 <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span> 1323 <span class="hljs-punctuation">}</span> 1324 <span class="hljs-punctuation">]</span> 1325 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1326 <span class="hljs-attr">"cost"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1574.14</span><span class="hljs-punctuation">,</span> 1327 <span class="hljs-attr">"afterGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1328 <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> 1329 <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">6053888</span><span class="hljs-punctuation">,</span> 1330 <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span> 1331 <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5500928</span><span class="hljs-punctuation">,</span> 1332 <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341101384</span><span class="hljs-punctuation">,</span> 1333 <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span> 1334 <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span> 1335 <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4059096</span><span class="hljs-punctuation">,</span> 1336 <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span> 1337 <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span> 1338 <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span> 1339 <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span> 1340 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span> 1341 <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 1342 <span class="hljs-punctuation">{</span> 1343 <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span> 1344 <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1345 <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1346 <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> 1347 <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</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> 1352 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> 1353 <span class="hljs-attr">"endTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059036865</span> 1354<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 1355<p>Here's an example.</p> 1356<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>); 1357<span class="hljs-keyword">const</span> profiler = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GCProfiler</span>(); 1358profiler.<span class="hljs-title function_">start</span>(); 1359<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1360 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(profiler.<span class="hljs-title function_">stop</span>()); 1361}, <span class="hljs-number">1000</span>);</code> <button class="copy-button">copy</button></pre></section> 1362 <!-- API END --> 1363 </div> 1364 </div> 1365 </div> 1366</body> 1367</html> 1368