xref: /third_party/node/doc/api/v8.html (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>V8 | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/v8.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  <style>@media(max-width:574px){.with-44-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-v8">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8 active">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="v8" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><a href="#v8">V8</a>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#v8cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li>
1261cb0ef41Sopenharmony_ci<li><a href="#v8getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li>
1271cb0ef41Sopenharmony_ci<li><a href="#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#v8getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li>
1291cb0ef41Sopenharmony_ci<li><a href="#v8getheapstatistics"><code>v8.getHeapStatistics()</code></a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#v8setflagsfromstringflags"><code>v8.setFlagsFromString(flags)</code></a></li>
1311cb0ef41Sopenharmony_ci<li><a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a></li>
1321cb0ef41Sopenharmony_ci<li><a href="#v8takecoverage"><code>v8.takeCoverage()</code></a></li>
1331cb0ef41Sopenharmony_ci<li><a href="#v8writeheapsnapshotfilename"><code>v8.writeHeapSnapshot([filename])</code></a></li>
1341cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#v8setheapsnapshotnearheaplimitlimit"><code>v8.setHeapSnapshotNearHeapLimit(limit)</code></a></span></li>
1351cb0ef41Sopenharmony_ci<li><a href="#serialization-api">Serialization API</a>
1361cb0ef41Sopenharmony_ci<ul>
1371cb0ef41Sopenharmony_ci<li><a href="#v8serializevalue"><code>v8.serialize(value)</code></a></li>
1381cb0ef41Sopenharmony_ci<li><a href="#v8deserializebuffer"><code>v8.deserialize(buffer)</code></a></li>
1391cb0ef41Sopenharmony_ci<li><a href="#class-v8serializer">Class: <code>v8.Serializer</code></a>
1401cb0ef41Sopenharmony_ci<ul>
1411cb0ef41Sopenharmony_ci<li><a href="#new-serializer"><code>new Serializer()</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#serializerwriteheader"><code>serializer.writeHeader()</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#serializerwritevaluevalue"><code>serializer.writeValue(value)</code></a></li>
1441cb0ef41Sopenharmony_ci<li><a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a></li>
1451cb0ef41Sopenharmony_ci<li><a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
1461cb0ef41Sopenharmony_ci<li><a href="#serializerwriteuint32value"><code>serializer.writeUint32(value)</code></a></li>
1471cb0ef41Sopenharmony_ci<li><a href="#serializerwriteuint64hi-lo"><code>serializer.writeUint64(hi, lo)</code></a></li>
1481cb0ef41Sopenharmony_ci<li><a href="#serializerwritedoublevalue"><code>serializer.writeDouble(value)</code></a></li>
1491cb0ef41Sopenharmony_ci<li><a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes(buffer)</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject(object)</code></a></li>
1511cb0ef41Sopenharmony_ci<li><a href="#serializer_getdatacloneerrormessage"><code>serializer._getDataCloneError(message)</code></a></li>
1521cb0ef41Sopenharmony_ci<li><a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#serializer_settreatarraybufferviewsashostobjectsflag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li>
1541cb0ef41Sopenharmony_ci</ul>
1551cb0ef41Sopenharmony_ci</li>
1561cb0ef41Sopenharmony_ci<li><a href="#class-v8deserializer">Class: <code>v8.Deserializer</code></a>
1571cb0ef41Sopenharmony_ci<ul>
1581cb0ef41Sopenharmony_ci<li><a href="#new-deserializerbuffer"><code>new Deserializer(buffer)</code></a></li>
1591cb0ef41Sopenharmony_ci<li><a href="#deserializerreadheader"><code>deserializer.readHeader()</code></a></li>
1601cb0ef41Sopenharmony_ci<li><a href="#deserializerreadvalue"><code>deserializer.readValue()</code></a></li>
1611cb0ef41Sopenharmony_ci<li><a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
1621cb0ef41Sopenharmony_ci<li><a href="#deserializergetwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li>
1631cb0ef41Sopenharmony_ci<li><a href="#deserializerreaduint32"><code>deserializer.readUint32()</code></a></li>
1641cb0ef41Sopenharmony_ci<li><a href="#deserializerreaduint64"><code>deserializer.readUint64()</code></a></li>
1651cb0ef41Sopenharmony_ci<li><a href="#deserializerreaddouble"><code>deserializer.readDouble()</code></a></li>
1661cb0ef41Sopenharmony_ci<li><a href="#deserializerreadrawbyteslength"><code>deserializer.readRawBytes(length)</code></a></li>
1671cb0ef41Sopenharmony_ci<li><a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li>
1681cb0ef41Sopenharmony_ci</ul>
1691cb0ef41Sopenharmony_ci</li>
1701cb0ef41Sopenharmony_ci<li><a href="#class-v8defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li>
1711cb0ef41Sopenharmony_ci<li><a href="#class-v8defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li>
1721cb0ef41Sopenharmony_ci</ul>
1731cb0ef41Sopenharmony_ci</li>
1741cb0ef41Sopenharmony_ci<li><a href="#promise-hooks">Promise hooks</a>
1751cb0ef41Sopenharmony_ci<ul>
1761cb0ef41Sopenharmony_ci<li><a href="#promisehooksoninitinit"><code>promiseHooks.onInit(init)</code></a></li>
1771cb0ef41Sopenharmony_ci<li><a href="#promisehooksonsettledsettled"><code>promiseHooks.onSettled(settled)</code></a></li>
1781cb0ef41Sopenharmony_ci<li><a href="#promisehooksonbeforebefore"><code>promiseHooks.onBefore(before)</code></a></li>
1791cb0ef41Sopenharmony_ci<li><a href="#promisehooksonafterafter"><code>promiseHooks.onAfter(after)</code></a></li>
1801cb0ef41Sopenharmony_ci<li><a href="#promisehookscreatehookcallbacks"><code>promiseHooks.createHook(callbacks)</code></a></li>
1811cb0ef41Sopenharmony_ci<li><a href="#hook-callbacks">Hook callbacks</a>
1821cb0ef41Sopenharmony_ci<ul>
1831cb0ef41Sopenharmony_ci<li><a href="#initpromise-parent"><code>init(promise, parent)</code></a></li>
1841cb0ef41Sopenharmony_ci<li><a href="#beforepromise"><code>before(promise)</code></a></li>
1851cb0ef41Sopenharmony_ci<li><a href="#afterpromise"><code>after(promise)</code></a></li>
1861cb0ef41Sopenharmony_ci<li><a href="#settledpromise"><code>settled(promise)</code></a></li>
1871cb0ef41Sopenharmony_ci</ul>
1881cb0ef41Sopenharmony_ci</li>
1891cb0ef41Sopenharmony_ci</ul>
1901cb0ef41Sopenharmony_ci</li>
1911cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#startup-snapshot-api">Startup Snapshot API</a></span>
1921cb0ef41Sopenharmony_ci<ul>
1931cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotaddserializecallbackcallback-data"><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code></a></li>
1941cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotadddeserializecallbackcallback-data"><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code></a></li>
1951cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotsetdeserializemainfunctioncallback-data"><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code></a></li>
1961cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotisbuildingsnapshot"><code>v8.startupSnapshot.isBuildingSnapshot()</code></a></li>
1971cb0ef41Sopenharmony_ci</ul>
1981cb0ef41Sopenharmony_ci</li>
1991cb0ef41Sopenharmony_ci<li><a href="#class-v8gcprofiler">Class: <code>v8.GCProfiler</code></a>
2001cb0ef41Sopenharmony_ci<ul>
2011cb0ef41Sopenharmony_ci<li><a href="#new-v8gcprofiler"><code>new v8.GCProfiler()</code></a></li>
2021cb0ef41Sopenharmony_ci<li><a href="#profilerstart"><code>profiler.start()</code></a></li>
2031cb0ef41Sopenharmony_ci<li><a href="#profilerstop"><code>profiler.stop()</code></a></li>
2041cb0ef41Sopenharmony_ci</ul>
2051cb0ef41Sopenharmony_ci</li>
2061cb0ef41Sopenharmony_ci</ul>
2071cb0ef41Sopenharmony_ci</li>
2081cb0ef41Sopenharmony_ci</ul></div></div>
2091cb0ef41Sopenharmony_ci    </li>
2101cb0ef41Sopenharmony_ci  
2111cb0ef41Sopenharmony_ci            
2121cb0ef41Sopenharmony_ci    <li class="picker-header">
2131cb0ef41Sopenharmony_ci      <a href="#">
2141cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2151cb0ef41Sopenharmony_ci        Index
2161cb0ef41Sopenharmony_ci      </a>
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci      <div class="picker"><ul>
2191cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci      <li>
2231cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
2241cb0ef41Sopenharmony_ci      </li>
2251cb0ef41Sopenharmony_ci    </ul>
2261cb0ef41Sopenharmony_ci  
2271cb0ef41Sopenharmony_ci<hr class="line">
2281cb0ef41Sopenharmony_ci<ul>
2291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
2571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
2701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8 active">V8</a></li>
2831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
2841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2891cb0ef41Sopenharmony_ci</ul>
2901cb0ef41Sopenharmony_ci<hr class="line">
2911cb0ef41Sopenharmony_ci<ul>
2921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2931cb0ef41Sopenharmony_ci</ul></div>
2941cb0ef41Sopenharmony_ci    </li>
2951cb0ef41Sopenharmony_ci  
2961cb0ef41Sopenharmony_ci            
2971cb0ef41Sopenharmony_ci    <li class="picker-header">
2981cb0ef41Sopenharmony_ci      <a href="#">
2991cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
3001cb0ef41Sopenharmony_ci        Other versions
3011cb0ef41Sopenharmony_ci      </a>
3021cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/v8.html">21.x</a></li>
3031cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/v8.html">20.x <b>LTS</b></a></li>
3041cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/v8.html">19.x</a></li>
3051cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/v8.html">18.x <b>LTS</b></a></li>
3061cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/v8.html">17.x</a></li>
3071cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/v8.html">16.x</a></li>
3081cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/v8.html">15.x</a></li>
3091cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/v8.html">14.x</a></li>
3101cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/v8.html">13.x</a></li>
3111cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/v8.html">12.x</a></li>
3121cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/v8.html">11.x</a></li>
3131cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/v8.html">10.x</a></li>
3141cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/v8.html">9.x</a></li>
3151cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/v8.html">8.x</a></li>
3161cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/v8.html">7.x</a></li>
3171cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/v8.html">6.x</a></li>
3181cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/v8.html">5.x</a></li>
3191cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/v8.html">4.x</a></li></ol></div>
3201cb0ef41Sopenharmony_ci    </li>
3211cb0ef41Sopenharmony_ci  
3221cb0ef41Sopenharmony_ci            <li class="picker-header">
3231cb0ef41Sopenharmony_ci              <a href="#">
3241cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
3251cb0ef41Sopenharmony_ci                Options
3261cb0ef41Sopenharmony_ci              </a>
3271cb0ef41Sopenharmony_ci        
3281cb0ef41Sopenharmony_ci              <div class="picker">
3291cb0ef41Sopenharmony_ci                <ul>
3301cb0ef41Sopenharmony_ci                  <li>
3311cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
3321cb0ef41Sopenharmony_ci                  </li>
3331cb0ef41Sopenharmony_ci                  <li>
3341cb0ef41Sopenharmony_ci                    <a href="v8.json">View as JSON</a>
3351cb0ef41Sopenharmony_ci                  </li>
3361cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/v8.md">Edit on GitHub</a></li>    
3371cb0ef41Sopenharmony_ci                </ul>
3381cb0ef41Sopenharmony_ci              </div>
3391cb0ef41Sopenharmony_ci            </li>
3401cb0ef41Sopenharmony_ci          </ul>
3411cb0ef41Sopenharmony_ci        </div>
3421cb0ef41Sopenharmony_ci        <hr>
3431cb0ef41Sopenharmony_ci      </header>
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3461cb0ef41Sopenharmony_ci<li><a href="#v8">V8</a>
3471cb0ef41Sopenharmony_ci<ul>
3481cb0ef41Sopenharmony_ci<li><a href="#v8cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li>
3491cb0ef41Sopenharmony_ci<li><a href="#v8getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li>
3501cb0ef41Sopenharmony_ci<li><a href="#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li>
3511cb0ef41Sopenharmony_ci<li><a href="#v8getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li>
3521cb0ef41Sopenharmony_ci<li><a href="#v8getheapstatistics"><code>v8.getHeapStatistics()</code></a></li>
3531cb0ef41Sopenharmony_ci<li><a href="#v8setflagsfromstringflags"><code>v8.setFlagsFromString(flags)</code></a></li>
3541cb0ef41Sopenharmony_ci<li><a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a></li>
3551cb0ef41Sopenharmony_ci<li><a href="#v8takecoverage"><code>v8.takeCoverage()</code></a></li>
3561cb0ef41Sopenharmony_ci<li><a href="#v8writeheapsnapshotfilename"><code>v8.writeHeapSnapshot([filename])</code></a></li>
3571cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#v8setheapsnapshotnearheaplimitlimit"><code>v8.setHeapSnapshotNearHeapLimit(limit)</code></a></span></li>
3581cb0ef41Sopenharmony_ci<li><a href="#serialization-api">Serialization API</a>
3591cb0ef41Sopenharmony_ci<ul>
3601cb0ef41Sopenharmony_ci<li><a href="#v8serializevalue"><code>v8.serialize(value)</code></a></li>
3611cb0ef41Sopenharmony_ci<li><a href="#v8deserializebuffer"><code>v8.deserialize(buffer)</code></a></li>
3621cb0ef41Sopenharmony_ci<li><a href="#class-v8serializer">Class: <code>v8.Serializer</code></a>
3631cb0ef41Sopenharmony_ci<ul>
3641cb0ef41Sopenharmony_ci<li><a href="#new-serializer"><code>new Serializer()</code></a></li>
3651cb0ef41Sopenharmony_ci<li><a href="#serializerwriteheader"><code>serializer.writeHeader()</code></a></li>
3661cb0ef41Sopenharmony_ci<li><a href="#serializerwritevaluevalue"><code>serializer.writeValue(value)</code></a></li>
3671cb0ef41Sopenharmony_ci<li><a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a></li>
3681cb0ef41Sopenharmony_ci<li><a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
3691cb0ef41Sopenharmony_ci<li><a href="#serializerwriteuint32value"><code>serializer.writeUint32(value)</code></a></li>
3701cb0ef41Sopenharmony_ci<li><a href="#serializerwriteuint64hi-lo"><code>serializer.writeUint64(hi, lo)</code></a></li>
3711cb0ef41Sopenharmony_ci<li><a href="#serializerwritedoublevalue"><code>serializer.writeDouble(value)</code></a></li>
3721cb0ef41Sopenharmony_ci<li><a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes(buffer)</code></a></li>
3731cb0ef41Sopenharmony_ci<li><a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject(object)</code></a></li>
3741cb0ef41Sopenharmony_ci<li><a href="#serializer_getdatacloneerrormessage"><code>serializer._getDataCloneError(message)</code></a></li>
3751cb0ef41Sopenharmony_ci<li><a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li>
3761cb0ef41Sopenharmony_ci<li><a href="#serializer_settreatarraybufferviewsashostobjectsflag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li>
3771cb0ef41Sopenharmony_ci</ul>
3781cb0ef41Sopenharmony_ci</li>
3791cb0ef41Sopenharmony_ci<li><a href="#class-v8deserializer">Class: <code>v8.Deserializer</code></a>
3801cb0ef41Sopenharmony_ci<ul>
3811cb0ef41Sopenharmony_ci<li><a href="#new-deserializerbuffer"><code>new Deserializer(buffer)</code></a></li>
3821cb0ef41Sopenharmony_ci<li><a href="#deserializerreadheader"><code>deserializer.readHeader()</code></a></li>
3831cb0ef41Sopenharmony_ci<li><a href="#deserializerreadvalue"><code>deserializer.readValue()</code></a></li>
3841cb0ef41Sopenharmony_ci<li><a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
3851cb0ef41Sopenharmony_ci<li><a href="#deserializergetwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li>
3861cb0ef41Sopenharmony_ci<li><a href="#deserializerreaduint32"><code>deserializer.readUint32()</code></a></li>
3871cb0ef41Sopenharmony_ci<li><a href="#deserializerreaduint64"><code>deserializer.readUint64()</code></a></li>
3881cb0ef41Sopenharmony_ci<li><a href="#deserializerreaddouble"><code>deserializer.readDouble()</code></a></li>
3891cb0ef41Sopenharmony_ci<li><a href="#deserializerreadrawbyteslength"><code>deserializer.readRawBytes(length)</code></a></li>
3901cb0ef41Sopenharmony_ci<li><a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li>
3911cb0ef41Sopenharmony_ci</ul>
3921cb0ef41Sopenharmony_ci</li>
3931cb0ef41Sopenharmony_ci<li><a href="#class-v8defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li>
3941cb0ef41Sopenharmony_ci<li><a href="#class-v8defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li>
3951cb0ef41Sopenharmony_ci</ul>
3961cb0ef41Sopenharmony_ci</li>
3971cb0ef41Sopenharmony_ci<li><a href="#promise-hooks">Promise hooks</a>
3981cb0ef41Sopenharmony_ci<ul>
3991cb0ef41Sopenharmony_ci<li><a href="#promisehooksoninitinit"><code>promiseHooks.onInit(init)</code></a></li>
4001cb0ef41Sopenharmony_ci<li><a href="#promisehooksonsettledsettled"><code>promiseHooks.onSettled(settled)</code></a></li>
4011cb0ef41Sopenharmony_ci<li><a href="#promisehooksonbeforebefore"><code>promiseHooks.onBefore(before)</code></a></li>
4021cb0ef41Sopenharmony_ci<li><a href="#promisehooksonafterafter"><code>promiseHooks.onAfter(after)</code></a></li>
4031cb0ef41Sopenharmony_ci<li><a href="#promisehookscreatehookcallbacks"><code>promiseHooks.createHook(callbacks)</code></a></li>
4041cb0ef41Sopenharmony_ci<li><a href="#hook-callbacks">Hook callbacks</a>
4051cb0ef41Sopenharmony_ci<ul>
4061cb0ef41Sopenharmony_ci<li><a href="#initpromise-parent"><code>init(promise, parent)</code></a></li>
4071cb0ef41Sopenharmony_ci<li><a href="#beforepromise"><code>before(promise)</code></a></li>
4081cb0ef41Sopenharmony_ci<li><a href="#afterpromise"><code>after(promise)</code></a></li>
4091cb0ef41Sopenharmony_ci<li><a href="#settledpromise"><code>settled(promise)</code></a></li>
4101cb0ef41Sopenharmony_ci</ul>
4111cb0ef41Sopenharmony_ci</li>
4121cb0ef41Sopenharmony_ci</ul>
4131cb0ef41Sopenharmony_ci</li>
4141cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#startup-snapshot-api">Startup Snapshot API</a></span>
4151cb0ef41Sopenharmony_ci<ul>
4161cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotaddserializecallbackcallback-data"><code>v8.startupSnapshot.addSerializeCallback(callback[, data])</code></a></li>
4171cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotadddeserializecallbackcallback-data"><code>v8.startupSnapshot.addDeserializeCallback(callback[, data])</code></a></li>
4181cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotsetdeserializemainfunctioncallback-data"><code>v8.startupSnapshot.setDeserializeMainFunction(callback[, data])</code></a></li>
4191cb0ef41Sopenharmony_ci<li><a href="#v8startupsnapshotisbuildingsnapshot"><code>v8.startupSnapshot.isBuildingSnapshot()</code></a></li>
4201cb0ef41Sopenharmony_ci</ul>
4211cb0ef41Sopenharmony_ci</li>
4221cb0ef41Sopenharmony_ci<li><a href="#class-v8gcprofiler">Class: <code>v8.GCProfiler</code></a>
4231cb0ef41Sopenharmony_ci<ul>
4241cb0ef41Sopenharmony_ci<li><a href="#new-v8gcprofiler"><code>new v8.GCProfiler()</code></a></li>
4251cb0ef41Sopenharmony_ci<li><a href="#profilerstart"><code>profiler.start()</code></a></li>
4261cb0ef41Sopenharmony_ci<li><a href="#profilerstop"><code>profiler.stop()</code></a></li>
4271cb0ef41Sopenharmony_ci</ul>
4281cb0ef41Sopenharmony_ci</li>
4291cb0ef41Sopenharmony_ci</ul>
4301cb0ef41Sopenharmony_ci</li>
4311cb0ef41Sopenharmony_ci</ul></details>
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci      <div id="apicontent">
4341cb0ef41Sopenharmony_ci        <h2>V8<span><a class="mark" href="#v8" id="v8">#</a></span><a aria-hidden="true" class="legacy" id="v8_v8"></a></h2>
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/v8.js">lib/v8.js</a></p>
4371cb0ef41Sopenharmony_ci<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>
4381cb0ef41Sopenharmony_cibuilt into the Node.js binary. It can be accessed using:</p>
4391cb0ef41Sopenharmony_ci<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>
4401cb0ef41Sopenharmony_ci<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>
4411cb0ef41Sopenharmony_ci<div class="api_metadata">
4421cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
4431cb0ef41Sopenharmony_ci</div>
4441cb0ef41Sopenharmony_ci<ul>
4451cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
4461cb0ef41Sopenharmony_ci</ul>
4471cb0ef41Sopenharmony_ci<p>Returns an integer representing a version tag derived from the V8 version,
4481cb0ef41Sopenharmony_cicommand-line flags, and detected CPU features. This is useful for determining
4491cb0ef41Sopenharmony_ciwhether a <a href="vm.html#new-vmscriptcode-options"><code>vm.Script</code></a> <code>cachedData</code> buffer is compatible with this instance
4501cb0ef41Sopenharmony_ciof V8.</p>
4511cb0ef41Sopenharmony_ci<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>
4521cb0ef41Sopenharmony_ci<span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V8</span>
4531cb0ef41Sopenharmony_ci<span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the value</span>
4541cb0ef41Sopenharmony_ci<span class="hljs-comment">// does indeed update when flags are toggled.</span>
4551cb0ef41Sopenharmony_civ8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_natives_syntax'</span>);
4561cb0ef41Sopenharmony_ci<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>
4571cb0ef41Sopenharmony_ci</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>
4581cb0ef41Sopenharmony_ci<div class="api_metadata">
4591cb0ef41Sopenharmony_ci<span>Added in: v12.8.0</span>
4601cb0ef41Sopenharmony_ci</div>
4611cb0ef41Sopenharmony_ci<ul>
4621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
4631cb0ef41Sopenharmony_ci</ul>
4641cb0ef41Sopenharmony_ci<p>Get statistics about code and its metadata in the heap, see V8
4651cb0ef41Sopenharmony_ci<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
4661cb0ef41Sopenharmony_cifollowing properties:</p>
4671cb0ef41Sopenharmony_ci<ul>
4681cb0ef41Sopenharmony_ci<li><code>code_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
4691cb0ef41Sopenharmony_ci<li><code>bytecode_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
4701cb0ef41Sopenharmony_ci<li><code>external_script_source_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
4711cb0ef41Sopenharmony_ci<li><code>cpu_profiler_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
4721cb0ef41Sopenharmony_ci</ul>
4731cb0ef41Sopenharmony_ci<!-- eslint-skip -->
4741cb0ef41Sopenharmony_ci<pre><code class="language-js">{
4751cb0ef41Sopenharmony_ci  <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>,
4761cb0ef41Sopenharmony_ci  <span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>,
4771cb0ef41Sopenharmony_ci  <span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</span>,
4781cb0ef41Sopenharmony_ci  <span class="hljs-attr">cpu_profiler_metadata_size</span>: <span class="hljs-number">0</span>,
4791cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
4801cb0ef41Sopenharmony_ci</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>
4811cb0ef41Sopenharmony_ci<div class="api_metadata">
4821cb0ef41Sopenharmony_ci<span>Added in: v11.13.0</span>
4831cb0ef41Sopenharmony_ci</div>
4841cb0ef41Sopenharmony_ci<ul>
4851cb0ef41Sopenharmony_ci<li>Returns: <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> A Readable Stream containing the V8 heap snapshot</li>
4861cb0ef41Sopenharmony_ci</ul>
4871cb0ef41Sopenharmony_ci<p>Generates a snapshot of the current V8 heap and returns a Readable
4881cb0ef41Sopenharmony_ciStream that may be used to read the JSON serialized representation.
4891cb0ef41Sopenharmony_ciThis JSON stream format is intended to be used with tools such as
4901cb0ef41Sopenharmony_ciChrome DevTools. The JSON schema is undocumented and specific to the
4911cb0ef41Sopenharmony_ciV8 engine. Therefore, the schema may change from one version of V8 to the next.</p>
4921cb0ef41Sopenharmony_ci<p>Creating a heap snapshot requires memory about twice the size of the heap at
4931cb0ef41Sopenharmony_cithe time the snapshot is created. This results in the risk of OOM killers
4941cb0ef41Sopenharmony_citerminating the process.</p>
4951cb0ef41Sopenharmony_ci<p>Generating a snapshot is a synchronous operation which blocks the event loop
4961cb0ef41Sopenharmony_cifor a duration depending on the heap size.</p>
4971cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</span>
4981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>);
4991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnapshot</span>();
5001cb0ef41Sopenharmony_cistream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code> <button class="copy-button">copy</button></pre>
5011cb0ef41Sopenharmony_ci</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>
5021cb0ef41Sopenharmony_ci<div class="api_metadata">
5031cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5041cb0ef41Sopenharmony_ci<table>
5051cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5061cb0ef41Sopenharmony_ci<tr><td>v7.5.0</td>
5071cb0ef41Sopenharmony_ci<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
5081cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
5091cb0ef41Sopenharmony_ci<td><p><span>Added in: v6.0.0</span></p></td></tr>
5101cb0ef41Sopenharmony_ci</tbody></table>
5111cb0ef41Sopenharmony_ci</details>
5121cb0ef41Sopenharmony_ci</div>
5131cb0ef41Sopenharmony_ci<ul>
5141cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a></li>
5151cb0ef41Sopenharmony_ci</ul>
5161cb0ef41Sopenharmony_ci<p>Returns statistics about the V8 heap spaces, i.e. the segments which make up
5171cb0ef41Sopenharmony_cithe V8 heap. Neither the ordering of heap spaces, nor the availability of a
5181cb0ef41Sopenharmony_ciheap space can be guaranteed as the statistics are provided via the V8
5191cb0ef41Sopenharmony_ci<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
5201cb0ef41Sopenharmony_cinext.</p>
5211cb0ef41Sopenharmony_ci<p>The value returned is an array of objects containing the following properties:</p>
5221cb0ef41Sopenharmony_ci<ul>
5231cb0ef41Sopenharmony_ci<li><code>space_name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
5241cb0ef41Sopenharmony_ci<li><code>space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5251cb0ef41Sopenharmony_ci<li><code>space_used_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5261cb0ef41Sopenharmony_ci<li><code>space_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5271cb0ef41Sopenharmony_ci<li><code>physical_space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5281cb0ef41Sopenharmony_ci</ul>
5291cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">[</span>
5301cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">{</span>
5311cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"new_space"</span><span class="hljs-punctuation">,</span>
5321cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span><span class="hljs-punctuation">,</span>
5331cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">951112</span><span class="hljs-punctuation">,</span>
5341cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">80824</span><span class="hljs-punctuation">,</span>
5351cb0ef41Sopenharmony_ci    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span>
5361cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
5371cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">{</span>
5381cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"old_space"</span><span class="hljs-punctuation">,</span>
5391cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span><span class="hljs-punctuation">,</span>
5401cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2493792</span><span class="hljs-punctuation">,</span>
5411cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
5421cb0ef41Sopenharmony_ci    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span>
5431cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
5441cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">{</span>
5451cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"code_space"</span><span class="hljs-punctuation">,</span>
5461cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span><span class="hljs-punctuation">,</span>
5471cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">644256</span><span class="hljs-punctuation">,</span>
5481cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">960</span><span class="hljs-punctuation">,</span>
5491cb0ef41Sopenharmony_ci    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span>
5501cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
5511cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">{</span>
5521cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"map_space"</span><span class="hljs-punctuation">,</span>
5531cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span><span class="hljs-punctuation">,</span>
5541cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">201608</span><span class="hljs-punctuation">,</span>
5551cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
5561cb0ef41Sopenharmony_ci    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span>
5571cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
5581cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">{</span>
5591cb0ef41Sopenharmony_ci    <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>
5601cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
5611cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
5621cb0ef41Sopenharmony_ci    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1490980608</span><span class="hljs-punctuation">,</span>
5631cb0ef41Sopenharmony_ci    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span>
5641cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">}</span>
5651cb0ef41Sopenharmony_ci<span class="hljs-punctuation">]</span></code> <button class="copy-button">copy</button></pre>
5661cb0ef41Sopenharmony_ci</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>
5671cb0ef41Sopenharmony_ci<div class="api_metadata">
5681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5691cb0ef41Sopenharmony_ci<table>
5701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5711cb0ef41Sopenharmony_ci<tr><td>v7.5.0</td>
5721cb0ef41Sopenharmony_ci<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
5731cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td>
5741cb0ef41Sopenharmony_ci<td><p>Added <code>malloced_memory</code>, <code>peak_malloced_memory</code>, and <code>does_zap_garbage</code>.</p></td></tr>
5751cb0ef41Sopenharmony_ci<tr><td>v1.0.0</td>
5761cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.0.0</span></p></td></tr>
5771cb0ef41Sopenharmony_ci</tbody></table>
5781cb0ef41Sopenharmony_ci</details>
5791cb0ef41Sopenharmony_ci</div>
5801cb0ef41Sopenharmony_ci<ul>
5811cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
5821cb0ef41Sopenharmony_ci</ul>
5831cb0ef41Sopenharmony_ci<p>Returns an object with the following properties:</p>
5841cb0ef41Sopenharmony_ci<ul>
5851cb0ef41Sopenharmony_ci<li><code>total_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5861cb0ef41Sopenharmony_ci<li><code>total_heap_size_executable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5871cb0ef41Sopenharmony_ci<li><code>total_physical_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5881cb0ef41Sopenharmony_ci<li><code>total_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5891cb0ef41Sopenharmony_ci<li><code>used_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5901cb0ef41Sopenharmony_ci<li><code>heap_size_limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5911cb0ef41Sopenharmony_ci<li><code>malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5921cb0ef41Sopenharmony_ci<li><code>peak_malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5931cb0ef41Sopenharmony_ci<li><code>does_zap_garbage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5941cb0ef41Sopenharmony_ci<li><code>number_of_native_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5951cb0ef41Sopenharmony_ci<li><code>number_of_detached_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5961cb0ef41Sopenharmony_ci<li><code>total_global_handles_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5971cb0ef41Sopenharmony_ci<li><code>used_global_handles_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5981cb0ef41Sopenharmony_ci<li><code>external_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
5991cb0ef41Sopenharmony_ci</ul>
6001cb0ef41Sopenharmony_ci<p><code>does_zap_garbage</code> is a 0/1 boolean, which signifies whether the
6011cb0ef41Sopenharmony_ci<code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap
6021cb0ef41Sopenharmony_cigarbage with a bit pattern. The RSS footprint (resident set size) gets bigger
6031cb0ef41Sopenharmony_cibecause it continuously touches all heap pages and that makes them less likely
6041cb0ef41Sopenharmony_cito get swapped out by the operating system.</p>
6051cb0ef41Sopenharmony_ci<p><code>number_of_native_contexts</code> The value of native_context is the number of the
6061cb0ef41Sopenharmony_citop-level contexts currently active. Increase of this number over time indicates
6071cb0ef41Sopenharmony_cia memory leak.</p>
6081cb0ef41Sopenharmony_ci<p><code>number_of_detached_contexts</code> The value of detached_context is the number
6091cb0ef41Sopenharmony_ciof contexts that were detached and not yet garbage collected. This number
6101cb0ef41Sopenharmony_cibeing non-zero indicates a potential memory leak.</p>
6111cb0ef41Sopenharmony_ci<p><code>total_global_handles_size</code> The value of total_global_handles_size is the
6121cb0ef41Sopenharmony_citotal memory size of V8 global handles.</p>
6131cb0ef41Sopenharmony_ci<p><code>used_global_handles_size</code> The value of used_global_handles_size is the
6141cb0ef41Sopenharmony_ciused memory size of V8 global handles.</p>
6151cb0ef41Sopenharmony_ci<p><code>external_memory</code> The value of external_memory is the memory size of array
6161cb0ef41Sopenharmony_cibuffers and external strings.</p>
6171cb0ef41Sopenharmony_ci<!-- eslint-skip -->
6181cb0ef41Sopenharmony_ci<pre><code class="language-js">{
6191cb0ef41Sopenharmony_ci  <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>,
6201cb0ef41Sopenharmony_ci  <span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span>,
6211cb0ef41Sopenharmony_ci  <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>,
6221cb0ef41Sopenharmony_ci  <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>,
6231cb0ef41Sopenharmony_ci  <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>,
6241cb0ef41Sopenharmony_ci  <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>,
6251cb0ef41Sopenharmony_ci  <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>,
6261cb0ef41Sopenharmony_ci  <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>,
6271cb0ef41Sopenharmony_ci  <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>,
6281cb0ef41Sopenharmony_ci  <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>,
6291cb0ef41Sopenharmony_ci  <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>,
6301cb0ef41Sopenharmony_ci  <span class="hljs-attr">total_global_handles_size</span>: <span class="hljs-number">8192</span>,
6311cb0ef41Sopenharmony_ci  <span class="hljs-attr">used_global_handles_size</span>: <span class="hljs-number">3296</span>,
6321cb0ef41Sopenharmony_ci  <span class="hljs-attr">external_memory</span>: <span class="hljs-number">318824</span>
6331cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
6341cb0ef41Sopenharmony_ci</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>
6351cb0ef41Sopenharmony_ci<div class="api_metadata">
6361cb0ef41Sopenharmony_ci<span>Added in: v1.0.0</span>
6371cb0ef41Sopenharmony_ci</div>
6381cb0ef41Sopenharmony_ci<ul>
6391cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
6401cb0ef41Sopenharmony_ci</ul>
6411cb0ef41Sopenharmony_ci<p>The <code>v8.setFlagsFromString()</code> method can be used to programmatically set
6421cb0ef41Sopenharmony_ciV8 command-line flags. This method should be used with care. Changing settings
6431cb0ef41Sopenharmony_ciafter the VM has started may result in unpredictable behavior, including
6441cb0ef41Sopenharmony_cicrashes and data loss; or it may simply do nothing.</p>
6451cb0ef41Sopenharmony_ci<p>The V8 options available for a version of Node.js may be determined by running
6461cb0ef41Sopenharmony_ci<code>node --v8-options</code>.</p>
6471cb0ef41Sopenharmony_ci<p>Usage:</p>
6481cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minute.</span>
6491cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>);
6501cb0ef41Sopenharmony_civ8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_gc'</span>);
6511cb0ef41Sopenharmony_ci<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>
6521cb0ef41Sopenharmony_ci</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>
6531cb0ef41Sopenharmony_ci<div class="api_metadata">
6541cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.18.0, v12.22.0</span>
6551cb0ef41Sopenharmony_ci</div>
6561cb0ef41Sopenharmony_ci<p>The <code>v8.stopCoverage()</code> method allows the user to stop the coverage collection
6571cb0ef41Sopenharmony_cistarted by <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>, so that V8 can release the execution count
6581cb0ef41Sopenharmony_cirecords and optimize code. This can be used in conjunction with
6591cb0ef41Sopenharmony_ci<a href="#v8takecoverage"><code>v8.takeCoverage()</code></a> if the user wants to collect the coverage on demand.</p>
6601cb0ef41Sopenharmony_ci</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>
6611cb0ef41Sopenharmony_ci<div class="api_metadata">
6621cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.18.0, v12.22.0</span>
6631cb0ef41Sopenharmony_ci</div>
6641cb0ef41Sopenharmony_ci<p>The <code>v8.takeCoverage()</code> method allows the user to write the coverage started by
6651cb0ef41Sopenharmony_ci<a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a> to disk on demand. This method can be invoked multiple
6661cb0ef41Sopenharmony_citimes during the lifetime of the process. Each time the execution counter will
6671cb0ef41Sopenharmony_cibe reset and a new coverage report will be written to the directory specified
6681cb0ef41Sopenharmony_ciby <a href="cli.html#node_v8_coveragedir"><code>NODE_V8_COVERAGE</code></a>.</p>
6691cb0ef41Sopenharmony_ci<p>When the process is about to exit, one last coverage will still be written to
6701cb0ef41Sopenharmony_cidisk unless <a href="#v8stopcoverage"><code>v8.stopCoverage()</code></a> is invoked before the process exits.</p>
6711cb0ef41Sopenharmony_ci</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>
6721cb0ef41Sopenharmony_ci<div class="api_metadata">
6731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
6741cb0ef41Sopenharmony_ci<table>
6751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
6761cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
6771cb0ef41Sopenharmony_ci<td><p>An exception will now be thrown if the file could not be written.</p></td></tr>
6781cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
6791cb0ef41Sopenharmony_ci<td><p>Make the returned error codes consistent across all platforms.</p></td></tr>
6801cb0ef41Sopenharmony_ci<tr><td>v11.13.0</td>
6811cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.13.0</span></p></td></tr>
6821cb0ef41Sopenharmony_ci</tbody></table>
6831cb0ef41Sopenharmony_ci</details>
6841cb0ef41Sopenharmony_ci</div>
6851cb0ef41Sopenharmony_ci<ul>
6861cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The file path where the V8 heap snapshot is to be
6871cb0ef41Sopenharmony_cisaved. If not specified, a file name with the pattern
6881cb0ef41Sopenharmony_ci<code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be
6891cb0ef41Sopenharmony_cigenerated, where <code>{pid}</code> will be the PID of the Node.js process,
6901cb0ef41Sopenharmony_ci<code>{thread_id}</code> will be <code>0</code> when <code>writeHeapSnapshot()</code> is called from
6911cb0ef41Sopenharmony_cithe main Node.js thread or the id of a worker thread.</li>
6921cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The filename where the snapshot was saved.</li>
6931cb0ef41Sopenharmony_ci</ul>
6941cb0ef41Sopenharmony_ci<p>Generates a snapshot of the current V8 heap and writes it to a JSON
6951cb0ef41Sopenharmony_cifile. This file is intended to be used with tools such as Chrome
6961cb0ef41Sopenharmony_ciDevTools. The JSON schema is undocumented and specific to the V8
6971cb0ef41Sopenharmony_ciengine, and may change from one version of V8 to the next.</p>
6981cb0ef41Sopenharmony_ci<p>A heap snapshot is specific to a single V8 isolate. When using
6991cb0ef41Sopenharmony_ci<a href="worker_threads.html">worker threads</a>, a heap snapshot generated from the main thread will
7001cb0ef41Sopenharmony_cinot contain any information about the workers, and vice versa.</p>
7011cb0ef41Sopenharmony_ci<p>Creating a heap snapshot requires memory about twice the size of the heap at
7021cb0ef41Sopenharmony_cithe time the snapshot is created. This results in the risk of OOM killers
7031cb0ef41Sopenharmony_citerminating the process.</p>
7041cb0ef41Sopenharmony_ci<p>Generating a snapshot is a synchronous operation which blocks the event loop
7051cb0ef41Sopenharmony_cifor a duration depending on the heap size.</p>
7061cb0ef41Sopenharmony_ci<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>);
7071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
7081cb0ef41Sopenharmony_ci  <span class="hljs-title class_">Worker</span>,
7091cb0ef41Sopenharmony_ci  isMainThread,
7101cb0ef41Sopenharmony_ci  parentPort,
7111cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) {
7141cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
7151cb0ef41Sopenharmony_ci
7161cb0ef41Sopenharmony_ci  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> {
7171cb0ef41Sopenharmony_ci    <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>);
7181cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Now get a heapdump for the main thread.</span>
7191cb0ef41Sopenharmony_ci    <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>);
7201cb0ef41Sopenharmony_ci  });
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Tell the worker to create a heapdump.</span>
7231cb0ef41Sopenharmony_ci  worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'</span>);
7241cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> {
7251cb0ef41Sopenharmony_ci  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> {
7261cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) {
7271cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Generate a heapdump for the worker</span>
7281cb0ef41Sopenharmony_ci      <span class="hljs-comment">// and return the filename to the parent.</span>
7291cb0ef41Sopenharmony_ci      parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">writeHeapSnapshot</span>());
7301cb0ef41Sopenharmony_ci    }
7311cb0ef41Sopenharmony_ci  });
7321cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
7331cb0ef41Sopenharmony_ci</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>
7341cb0ef41Sopenharmony_ci<div class="api_metadata">
7351cb0ef41Sopenharmony_ci<span>Added in: v18.10.0</span>
7361cb0ef41Sopenharmony_ci</div>
7371cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
7381cb0ef41Sopenharmony_ci<ul>
7391cb0ef41Sopenharmony_ci<li><code>limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
7401cb0ef41Sopenharmony_ci</ul>
7411cb0ef41Sopenharmony_ci<p>The API is a no-op if <code>--heapsnapshot-near-heap-limit</code> is already set from the
7421cb0ef41Sopenharmony_cicommand line or the API is called more than once. <code>limit</code> must be a positive
7431cb0ef41Sopenharmony_ciinteger. See <a href="cli.html#--heapsnapshot-near-heap-limitmax_count"><code>--heapsnapshot-near-heap-limit</code></a> for more information.</p>
7441cb0ef41Sopenharmony_ci</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>
7451cb0ef41Sopenharmony_ci<p>The serialization API provides means of serializing JavaScript values in a way
7461cb0ef41Sopenharmony_cithat 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>
7471cb0ef41Sopenharmony_ci<p>The format is backward-compatible (i.e. safe to store to disk).
7481cb0ef41Sopenharmony_ciEqual JavaScript values may result in different serialized output.</p>
7491cb0ef41Sopenharmony_ci<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>
7501cb0ef41Sopenharmony_ci<div class="api_metadata">
7511cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
7521cb0ef41Sopenharmony_ci</div>
7531cb0ef41Sopenharmony_ci<ul>
7541cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
7551cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
7561cb0ef41Sopenharmony_ci</ul>
7571cb0ef41Sopenharmony_ci<p>Uses a <a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a> to serialize <code>value</code> into a buffer.</p>
7581cb0ef41Sopenharmony_ci<p><a href="errors.html#err_buffer_too_large"><code>ERR_BUFFER_TOO_LARGE</code></a> will be thrown when trying to
7591cb0ef41Sopenharmony_ciserialize a huge object which requires buffer
7601cb0ef41Sopenharmony_cilarger than <a href="buffer.html#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p>
7611cb0ef41Sopenharmony_ci<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>
7621cb0ef41Sopenharmony_ci<div class="api_metadata">
7631cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
7641cb0ef41Sopenharmony_ci</div>
7651cb0ef41Sopenharmony_ci<ul>
7661cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A buffer returned by <a href="#v8serializevalue"><code>serialize()</code></a>.</li>
7671cb0ef41Sopenharmony_ci</ul>
7681cb0ef41Sopenharmony_ci<p>Uses a <a href="#class-v8defaultdeserializer"><code>DefaultDeserializer</code></a> with default options to read a JS value
7691cb0ef41Sopenharmony_cifrom a buffer.</p>
7701cb0ef41Sopenharmony_ci<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>
7711cb0ef41Sopenharmony_ci<div class="api_metadata">
7721cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
7731cb0ef41Sopenharmony_ci</div>
7741cb0ef41Sopenharmony_ci<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>
7751cb0ef41Sopenharmony_ci<p>Creates a new <code>Serializer</code> object.</p>
7761cb0ef41Sopenharmony_ci<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>
7771cb0ef41Sopenharmony_ci<p>Writes out a header, which includes the serialization format version.</p>
7781cb0ef41Sopenharmony_ci<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>
7791cb0ef41Sopenharmony_ci<ul>
7801cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
7811cb0ef41Sopenharmony_ci</ul>
7821cb0ef41Sopenharmony_ci<p>Serializes a JavaScript value and adds the serialized representation to the
7831cb0ef41Sopenharmony_ciinternal buffer.</p>
7841cb0ef41Sopenharmony_ci<p>This throws an error if <code>value</code> cannot be serialized.</p>
7851cb0ef41Sopenharmony_ci<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>
7861cb0ef41Sopenharmony_ci<ul>
7871cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
7881cb0ef41Sopenharmony_ci</ul>
7891cb0ef41Sopenharmony_ci<p>Returns the stored internal buffer. This serializer should not be used once
7901cb0ef41Sopenharmony_cithe buffer is released. Calling this method results in undefined behavior
7911cb0ef41Sopenharmony_ciif a previous write has failed.</p>
7921cb0ef41Sopenharmony_ci<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>
7931cb0ef41Sopenharmony_ci<ul>
7941cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> A 32-bit unsigned integer.</li>
7951cb0ef41Sopenharmony_ci<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> An <code>ArrayBuffer</code> instance.</li>
7961cb0ef41Sopenharmony_ci</ul>
7971cb0ef41Sopenharmony_ci<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band.
7981cb0ef41Sopenharmony_ciPass the corresponding <code>ArrayBuffer</code> in the deserializing context to
7991cb0ef41Sopenharmony_ci<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p>
8001cb0ef41Sopenharmony_ci<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>
8011cb0ef41Sopenharmony_ci<ul>
8021cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
8031cb0ef41Sopenharmony_ci</ul>
8041cb0ef41Sopenharmony_ci<p>Write a raw 32-bit unsigned integer.
8051cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p>
8061cb0ef41Sopenharmony_ci<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>
8071cb0ef41Sopenharmony_ci<ul>
8081cb0ef41Sopenharmony_ci<li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
8091cb0ef41Sopenharmony_ci<li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
8101cb0ef41Sopenharmony_ci</ul>
8111cb0ef41Sopenharmony_ci<p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
8121cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p>
8131cb0ef41Sopenharmony_ci<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>
8141cb0ef41Sopenharmony_ci<ul>
8151cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
8161cb0ef41Sopenharmony_ci</ul>
8171cb0ef41Sopenharmony_ci<p>Write a JS <code>number</code> value.
8181cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p>
8191cb0ef41Sopenharmony_ci<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>
8201cb0ef41Sopenharmony_ci<ul>
8211cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
8221cb0ef41Sopenharmony_ci</ul>
8231cb0ef41Sopenharmony_ci<p>Write raw bytes into the serializer's internal buffer. The deserializer
8241cb0ef41Sopenharmony_ciwill require a way to compute the length of the buffer.
8251cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p>
8261cb0ef41Sopenharmony_ci<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>
8271cb0ef41Sopenharmony_ci<ul>
8281cb0ef41Sopenharmony_ci<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
8291cb0ef41Sopenharmony_ci</ul>
8301cb0ef41Sopenharmony_ci<p>This method is called to write some kind of host object, i.e. an object created
8311cb0ef41Sopenharmony_ciby native C++ bindings. If it is not possible to serialize <code>object</code>, a suitable
8321cb0ef41Sopenharmony_ciexception should be thrown.</p>
8331cb0ef41Sopenharmony_ci<p>This method is not present on the <code>Serializer</code> class itself but can be provided
8341cb0ef41Sopenharmony_ciby subclasses.</p>
8351cb0ef41Sopenharmony_ci<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>
8361cb0ef41Sopenharmony_ci<ul>
8371cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
8381cb0ef41Sopenharmony_ci</ul>
8391cb0ef41Sopenharmony_ci<p>This method is called to generate error objects that will be thrown when an
8401cb0ef41Sopenharmony_ciobject can not be cloned.</p>
8411cb0ef41Sopenharmony_ci<p>This method defaults to the <a href="errors.html#class-error"><code>Error</code></a> constructor and can be overridden on
8421cb0ef41Sopenharmony_cisubclasses.</p>
8431cb0ef41Sopenharmony_ci<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>
8441cb0ef41Sopenharmony_ci<ul>
8451cb0ef41Sopenharmony_ci<li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type">&#x3C;SharedArrayBuffer></a></li>
8461cb0ef41Sopenharmony_ci</ul>
8471cb0ef41Sopenharmony_ci<p>This method is called when the serializer is going to serialize a
8481cb0ef41Sopenharmony_ci<code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for
8491cb0ef41Sopenharmony_cithe object, using the same ID if this <code>SharedArrayBuffer</code> has already been
8501cb0ef41Sopenharmony_ciserialized. When deserializing, this ID will be passed to
8511cb0ef41Sopenharmony_ci<a href="#deserializertransferarraybufferid-arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p>
8521cb0ef41Sopenharmony_ci<p>If the object cannot be serialized, an exception should be thrown.</p>
8531cb0ef41Sopenharmony_ci<p>This method is not present on the <code>Serializer</code> class itself but can be provided
8541cb0ef41Sopenharmony_ciby subclasses.</p>
8551cb0ef41Sopenharmony_ci<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>
8561cb0ef41Sopenharmony_ci<ul>
8571cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
8581cb0ef41Sopenharmony_ci</ul>
8591cb0ef41Sopenharmony_ci<p>Indicate whether to treat <code>TypedArray</code> and <code>DataView</code> objects as
8601cb0ef41Sopenharmony_cihost objects, i.e. pass them to <a href="#serializer_writehostobjectobject"><code>serializer._writeHostObject()</code></a>.</p>
8611cb0ef41Sopenharmony_ci<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>
8621cb0ef41Sopenharmony_ci<div class="api_metadata">
8631cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
8641cb0ef41Sopenharmony_ci</div>
8651cb0ef41Sopenharmony_ci<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>
8661cb0ef41Sopenharmony_ci<ul>
8671cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A buffer returned by
8681cb0ef41Sopenharmony_ci<a href="#serializerreleasebuffer"><code>serializer.releaseBuffer()</code></a>.</li>
8691cb0ef41Sopenharmony_ci</ul>
8701cb0ef41Sopenharmony_ci<p>Creates a new <code>Deserializer</code> object.</p>
8711cb0ef41Sopenharmony_ci<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>
8721cb0ef41Sopenharmony_ci<p>Reads and validates a header (including the format version).
8731cb0ef41Sopenharmony_ciMay, for example, reject an invalid or unsupported wire format. In that case,
8741cb0ef41Sopenharmony_cian <code>Error</code> is thrown.</p>
8751cb0ef41Sopenharmony_ci<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>
8761cb0ef41Sopenharmony_ci<p>Deserializes a JavaScript value from the buffer and returns it.</p>
8771cb0ef41Sopenharmony_ci<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>
8781cb0ef41Sopenharmony_ci<ul>
8791cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> A 32-bit unsigned integer.</li>
8801cb0ef41Sopenharmony_ci<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type">&#x3C;SharedArrayBuffer></a> An <code>ArrayBuffer</code> instance.</li>
8811cb0ef41Sopenharmony_ci</ul>
8821cb0ef41Sopenharmony_ci<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band.
8831cb0ef41Sopenharmony_ciPass the corresponding <code>ArrayBuffer</code> in the serializing context to
8841cb0ef41Sopenharmony_ci<a href="#serializertransferarraybufferid-arraybuffer"><code>serializer.transferArrayBuffer()</code></a> (or return the <code>id</code> from
8851cb0ef41Sopenharmony_ci<a href="#serializer_getsharedarraybufferidsharedarraybuffer"><code>serializer._getSharedArrayBufferId()</code></a> in the case of <code>SharedArrayBuffer</code>s).</p>
8861cb0ef41Sopenharmony_ci<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>
8871cb0ef41Sopenharmony_ci<ul>
8881cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
8891cb0ef41Sopenharmony_ci</ul>
8901cb0ef41Sopenharmony_ci<p>Reads the underlying wire format version. Likely mostly to be useful to
8911cb0ef41Sopenharmony_cilegacy code reading old wire format versions. May not be called before
8921cb0ef41Sopenharmony_ci<code>.readHeader()</code>.</p>
8931cb0ef41Sopenharmony_ci<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>
8941cb0ef41Sopenharmony_ci<ul>
8951cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
8961cb0ef41Sopenharmony_ci</ul>
8971cb0ef41Sopenharmony_ci<p>Read a raw 32-bit unsigned integer and return it.
8981cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
8991cb0ef41Sopenharmony_ci<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>
9001cb0ef41Sopenharmony_ci<ul>
9011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer[]></a></li>
9021cb0ef41Sopenharmony_ci</ul>
9031cb0ef41Sopenharmony_ci<p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code>
9041cb0ef41Sopenharmony_ciwith two 32-bit unsigned integer entries.
9051cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
9061cb0ef41Sopenharmony_ci<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>
9071cb0ef41Sopenharmony_ci<ul>
9081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
9091cb0ef41Sopenharmony_ci</ul>
9101cb0ef41Sopenharmony_ci<p>Read a JS <code>number</code> value.
9111cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
9121cb0ef41Sopenharmony_ci<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>
9131cb0ef41Sopenharmony_ci<ul>
9141cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
9151cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
9161cb0ef41Sopenharmony_ci</ul>
9171cb0ef41Sopenharmony_ci<p>Read raw bytes from the deserializer's internal buffer. The <code>length</code> parameter
9181cb0ef41Sopenharmony_cimust correspond to the length of the buffer that was passed to
9191cb0ef41Sopenharmony_ci<a href="#serializerwriterawbytesbuffer"><code>serializer.writeRawBytes()</code></a>.
9201cb0ef41Sopenharmony_ciFor use inside of a custom <a href="#deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
9211cb0ef41Sopenharmony_ci<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>
9221cb0ef41Sopenharmony_ci<p>This method is called to read some kind of host object, i.e. an object that is
9231cb0ef41Sopenharmony_cicreated by native C++ bindings. If it is not possible to deserialize the data,
9241cb0ef41Sopenharmony_cia suitable exception should be thrown.</p>
9251cb0ef41Sopenharmony_ci<p>This method is not present on the <code>Deserializer</code> class itself but can be
9261cb0ef41Sopenharmony_ciprovided by subclasses.</p>
9271cb0ef41Sopenharmony_ci<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>
9281cb0ef41Sopenharmony_ci<div class="api_metadata">
9291cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
9301cb0ef41Sopenharmony_ci</div>
9311cb0ef41Sopenharmony_ci<p>A subclass of <a href="#class-v8serializer"><code>Serializer</code></a> that serializes <code>TypedArray</code>
9321cb0ef41Sopenharmony_ci(in particular <a href="buffer.html"><code>Buffer</code></a>) and <code>DataView</code> objects as host objects, and only
9331cb0ef41Sopenharmony_cistores the part of their underlying <code>ArrayBuffer</code>s that they are referring to.</p>
9341cb0ef41Sopenharmony_ci<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>
9351cb0ef41Sopenharmony_ci<div class="api_metadata">
9361cb0ef41Sopenharmony_ci<span>Added in: v8.0.0</span>
9371cb0ef41Sopenharmony_ci</div>
9381cb0ef41Sopenharmony_ci<p>A subclass of <a href="#class-v8deserializer"><code>Deserializer</code></a> corresponding to the format written by
9391cb0ef41Sopenharmony_ci<a href="#class-v8defaultserializer"><code>DefaultSerializer</code></a>.</p>
9401cb0ef41Sopenharmony_ci</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>
9411cb0ef41Sopenharmony_ci<p>The <code>promiseHooks</code> interface can be used to track promise lifecycle events.
9421cb0ef41Sopenharmony_ciTo track <em>all</em> async activity, see <a href="async_hooks.html"><code>async_hooks</code></a> which internally uses this
9431cb0ef41Sopenharmony_cimodule to produce promise lifecycle events in addition to events for other
9441cb0ef41Sopenharmony_ciasync resources. For request context management, see <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.</p>
9451cb0ef41Sopenharmony_ci<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>;
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ci<span class="hljs-comment">// There are four lifecycle events produced by promises:</span>
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ci<span class="hljs-comment">// The `init` event represents the creation of a promise. This could be a</span>
9501cb0ef41Sopenharmony_ci<span class="hljs-comment">// direct creation such as with `new Promise(...)` or a continuation such</span>
9511cb0ef41Sopenharmony_ci<span class="hljs-comment">// as `then()` or `catch()`. It also happens whenever an async function is</span>
9521cb0ef41Sopenharmony_ci<span class="hljs-comment">// called or does an `await`. If a continuation promise is created, the</span>
9531cb0ef41Sopenharmony_ci<span class="hljs-comment">// `parent` will be the promise it is a continuation from.</span>
9541cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {
9551cb0ef41Sopenharmony_ci  <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 });
9561cb0ef41Sopenharmony_ci}
9571cb0ef41Sopenharmony_ci
9581cb0ef41Sopenharmony_ci<span class="hljs-comment">// The `settled` event happens when a promise receives a resolution or</span>
9591cb0ef41Sopenharmony_ci<span class="hljs-comment">// rejection value. This may happen synchronously such as when using</span>
9601cb0ef41Sopenharmony_ci<span class="hljs-comment">// `Promise.resolve()` on non-promise input.</span>
9611cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">settled</span>(<span class="hljs-params">promise</span>) {
9621cb0ef41Sopenharmony_ci  <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 });
9631cb0ef41Sopenharmony_ci}
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ci<span class="hljs-comment">// The `before` event runs immediately before a `then()` or `catch()` handler</span>
9661cb0ef41Sopenharmony_ci<span class="hljs-comment">// runs or an `await` resumes execution.</span>
9671cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">promise</span>) {
9681cb0ef41Sopenharmony_ci  <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 });
9691cb0ef41Sopenharmony_ci}
9701cb0ef41Sopenharmony_ci
9711cb0ef41Sopenharmony_ci<span class="hljs-comment">// The `after` event runs immediately after a `then()` handler runs or when</span>
9721cb0ef41Sopenharmony_ci<span class="hljs-comment">// an `await` begins after resuming from another.</span>
9731cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">promise</span>) {
9741cb0ef41Sopenharmony_ci  <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 });
9751cb0ef41Sopenharmony_ci}
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Lifecycle hooks may be started and stopped individually</span>
9781cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopWatchingInits = promiseHooks.<span class="hljs-title function_">onInit</span>(init);
9791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopWatchingSettleds = promiseHooks.<span class="hljs-title function_">onSettled</span>(settled);
9801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopWatchingBefores = promiseHooks.<span class="hljs-title function_">onBefore</span>(before);
9811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopWatchingAfters = promiseHooks.<span class="hljs-title function_">onAfter</span>(after);
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Or they may be started and stopped in groups</span>
9841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopHookSet = promiseHooks.<span class="hljs-title function_">createHook</span>({
9851cb0ef41Sopenharmony_ci  init,
9861cb0ef41Sopenharmony_ci  settled,
9871cb0ef41Sopenharmony_ci  before,
9881cb0ef41Sopenharmony_ci  after,
9891cb0ef41Sopenharmony_ci});
9901cb0ef41Sopenharmony_ci
9911cb0ef41Sopenharmony_ci<span class="hljs-comment">// To stop a hook, call the function returned at its creation.</span>
9921cb0ef41Sopenharmony_ci<span class="hljs-title function_">stopWatchingInits</span>();
9931cb0ef41Sopenharmony_ci<span class="hljs-title function_">stopWatchingSettleds</span>();
9941cb0ef41Sopenharmony_ci<span class="hljs-title function_">stopWatchingBefores</span>();
9951cb0ef41Sopenharmony_ci<span class="hljs-title function_">stopWatchingAfters</span>();
9961cb0ef41Sopenharmony_ci<span class="hljs-title function_">stopHookSet</span>();</code> <button class="copy-button">copy</button></pre>
9971cb0ef41Sopenharmony_ci<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>
9981cb0ef41Sopenharmony_ci<div class="api_metadata">
9991cb0ef41Sopenharmony_ci<span>Added in: v17.1.0, v16.14.0</span>
10001cb0ef41Sopenharmony_ci</div>
10011cb0ef41Sopenharmony_ci<ul>
10021cb0ef41Sopenharmony_ci<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#initpromise-parent"><code>init</code> callback</a> to call when a promise is created.</li>
10031cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Call to stop the hook.</li>
10041cb0ef41Sopenharmony_ci</ul>
10051cb0ef41Sopenharmony_ci<p><strong>The <code>init</code> hook must be a plain function. Providing an async function will
10061cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.</strong></p>
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_ci<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>;
10091cb0ef41Sopenharmony_ci
10101cb0ef41Sopenharmony_ci<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>);
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_ci<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>
10131cb0ef41Sopenharmony_ci<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>
10141cb0ef41Sopenharmony_ci<div class="api_metadata">
10151cb0ef41Sopenharmony_ci<span>Added in: v17.1.0, v16.14.0</span>
10161cb0ef41Sopenharmony_ci</div>
10171cb0ef41Sopenharmony_ci<ul>
10181cb0ef41Sopenharmony_ci<li><code>settled</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#settledpromise"><code>settled</code> callback</a> to call when a promise
10191cb0ef41Sopenharmony_ciis resolved or rejected.</li>
10201cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Call to stop the hook.</li>
10211cb0ef41Sopenharmony_ci</ul>
10221cb0ef41Sopenharmony_ci<p><strong>The <code>settled</code> hook must be a plain function. Providing an async function will
10231cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.</strong></p>
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci<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>;
10261cb0ef41Sopenharmony_ci
10271cb0ef41Sopenharmony_ci<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>);
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_ci<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>
10301cb0ef41Sopenharmony_ci<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>
10311cb0ef41Sopenharmony_ci<div class="api_metadata">
10321cb0ef41Sopenharmony_ci<span>Added in: v17.1.0, v16.14.0</span>
10331cb0ef41Sopenharmony_ci</div>
10341cb0ef41Sopenharmony_ci<ul>
10351cb0ef41Sopenharmony_ci<li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#beforepromise"><code>before</code> callback</a> to call before a promise
10361cb0ef41Sopenharmony_cicontinuation executes.</li>
10371cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Call to stop the hook.</li>
10381cb0ef41Sopenharmony_ci</ul>
10391cb0ef41Sopenharmony_ci<p><strong>The <code>before</code> hook must be a plain function. Providing an async function will
10401cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.</strong></p>
10411cb0ef41Sopenharmony_ci
10421cb0ef41Sopenharmony_ci<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>;
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci<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>);
10451cb0ef41Sopenharmony_ci
10461cb0ef41Sopenharmony_ci<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>
10471cb0ef41Sopenharmony_ci<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>
10481cb0ef41Sopenharmony_ci<div class="api_metadata">
10491cb0ef41Sopenharmony_ci<span>Added in: v17.1.0, v16.14.0</span>
10501cb0ef41Sopenharmony_ci</div>
10511cb0ef41Sopenharmony_ci<ul>
10521cb0ef41Sopenharmony_ci<li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#afterpromise"><code>after</code> callback</a> to call after a promise
10531cb0ef41Sopenharmony_cicontinuation executes.</li>
10541cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Call to stop the hook.</li>
10551cb0ef41Sopenharmony_ci</ul>
10561cb0ef41Sopenharmony_ci<p><strong>The <code>after</code> hook must be a plain function. Providing an async function will
10571cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.</strong></p>
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci<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>;
10601cb0ef41Sopenharmony_ci
10611cb0ef41Sopenharmony_ci<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>);
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ci<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>
10641cb0ef41Sopenharmony_ci<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>
10651cb0ef41Sopenharmony_ci<div class="api_metadata">
10661cb0ef41Sopenharmony_ci<span>Added in: v17.1.0, v16.14.0</span>
10671cb0ef41Sopenharmony_ci</div>
10681cb0ef41Sopenharmony_ci<ul>
10691cb0ef41Sopenharmony_ci<li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#hook-callbacks">Hook Callbacks</a> to register
10701cb0ef41Sopenharmony_ci<ul>
10711cb0ef41Sopenharmony_ci<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#initpromise-parent"><code>init</code> callback</a>.</li>
10721cb0ef41Sopenharmony_ci<li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#beforepromise"><code>before</code> callback</a>.</li>
10731cb0ef41Sopenharmony_ci<li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#afterpromise"><code>after</code> callback</a>.</li>
10741cb0ef41Sopenharmony_ci<li><code>settled</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#settledpromise"><code>settled</code> callback</a>.</li>
10751cb0ef41Sopenharmony_ci</ul>
10761cb0ef41Sopenharmony_ci</li>
10771cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Used for disabling hooks</li>
10781cb0ef41Sopenharmony_ci</ul>
10791cb0ef41Sopenharmony_ci<p><strong>The hook callbacks must be plain functions. Providing async functions will
10801cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.</strong></p>
10811cb0ef41Sopenharmony_ci<p>Registers functions to be called for different lifetime events of each promise.</p>
10821cb0ef41Sopenharmony_ci<p>The callbacks <code>init()</code>/<code>before()</code>/<code>after()</code>/<code>settled()</code> are called for the
10831cb0ef41Sopenharmony_cirespective events during a promise's lifetime.</p>
10841cb0ef41Sopenharmony_ci<p>All callbacks are optional. For example, if only promise creation needs to
10851cb0ef41Sopenharmony_cibe tracked, then only the <code>init</code> callback needs to be passed. The
10861cb0ef41Sopenharmony_cispecifics of all functions that can be passed to <code>callbacks</code> is in the
10871cb0ef41Sopenharmony_ci<a href="#hook-callbacks">Hook Callbacks</a> section.</p>
10881cb0ef41Sopenharmony_ci
10891cb0ef41Sopenharmony_ci<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>;
10901cb0ef41Sopenharmony_ci
10911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({
10921cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {},
10931cb0ef41Sopenharmony_ci});</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>);
10941cb0ef41Sopenharmony_ci
10951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stopAll = promiseHooks.<span class="hljs-title function_">createHook</span>({
10961cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">promise, parent</span>) {},
10971cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
10981cb0ef41Sopenharmony_ci<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>
10991cb0ef41Sopenharmony_ci<p>Key events in the lifetime of a promise have been categorized into four areas:
11001cb0ef41Sopenharmony_cicreation of a promise, before/after a continuation handler is called or around
11011cb0ef41Sopenharmony_cian await, and when the promise resolves or rejects.</p>
11021cb0ef41Sopenharmony_ci<p>While these hooks are similar to those of <a href="async_hooks.html"><code>async_hooks</code></a> they lack a
11031cb0ef41Sopenharmony_ci<code>destroy</code> hook. Other types of async resources typically represent sockets or
11041cb0ef41Sopenharmony_cifile descriptors which have a distinct "closed" state to express the <code>destroy</code>
11051cb0ef41Sopenharmony_cilifecycle event while promises remain usable for as long as code can still
11061cb0ef41Sopenharmony_cireach them. Garbage collection tracking is used to make promises fit into the
11071cb0ef41Sopenharmony_ci<code>async_hooks</code> event model, however this tracking is very expensive and they may
11081cb0ef41Sopenharmony_cinot necessarily ever even be garbage collected.</p>
11091cb0ef41Sopenharmony_ci<p>Because promises are asynchronous resources whose lifecycle is tracked
11101cb0ef41Sopenharmony_civia the promise hooks mechanism, the <code>init()</code>, <code>before()</code>, <code>after()</code>, and
11111cb0ef41Sopenharmony_ci<code>settled()</code> callbacks <em>must not</em> be async functions as they create more
11121cb0ef41Sopenharmony_cipromises which would produce an infinite loop.</p>
11131cb0ef41Sopenharmony_ci<p>While this API is used to feed promise events into <a href="async_hooks.html"><code>async_hooks</code></a>, the
11141cb0ef41Sopenharmony_ciordering between the two is undefined. Both APIs are multi-tenant
11151cb0ef41Sopenharmony_ciand therefore could produce events in any order relative to each other.</p>
11161cb0ef41Sopenharmony_ci<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>
11171cb0ef41Sopenharmony_ci<ul>
11181cb0ef41Sopenharmony_ci<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> The promise being created.</li>
11191cb0ef41Sopenharmony_ci<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> The promise continued from, if applicable.</li>
11201cb0ef41Sopenharmony_ci</ul>
11211cb0ef41Sopenharmony_ci<p>Called when a promise is constructed. This <em>does not</em> mean that corresponding
11221cb0ef41Sopenharmony_ci<code>before</code>/<code>after</code> events will occur, only that the possibility exists. This will
11231cb0ef41Sopenharmony_cihappen if a promise is created without ever getting a continuation.</p>
11241cb0ef41Sopenharmony_ci<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>
11251cb0ef41Sopenharmony_ci<ul>
11261cb0ef41Sopenharmony_ci<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
11271cb0ef41Sopenharmony_ci</ul>
11281cb0ef41Sopenharmony_ci<p>Called before a promise continuation executes. This can be in the form of
11291cb0ef41Sopenharmony_ci<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handlers or an <code>await</code> resuming.</p>
11301cb0ef41Sopenharmony_ci<p>The <code>before</code> callback will be called 0 to N times. The <code>before</code> callback
11311cb0ef41Sopenharmony_ciwill typically be called 0 times if no continuation was ever made for the
11321cb0ef41Sopenharmony_cipromise. The <code>before</code> callback may be called many times in the case where
11331cb0ef41Sopenharmony_cimany continuations have been made from the same promise.</p>
11341cb0ef41Sopenharmony_ci<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>
11351cb0ef41Sopenharmony_ci<ul>
11361cb0ef41Sopenharmony_ci<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
11371cb0ef41Sopenharmony_ci</ul>
11381cb0ef41Sopenharmony_ci<p>Called immediately after a promise continuation executes. This may be after a
11391cb0ef41Sopenharmony_ci<code>then()</code>, <code>catch()</code>, or <code>finally()</code> handler or before an <code>await</code> after another
11401cb0ef41Sopenharmony_ci<code>await</code>.</p>
11411cb0ef41Sopenharmony_ci<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>
11421cb0ef41Sopenharmony_ci<ul>
11431cb0ef41Sopenharmony_ci<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
11441cb0ef41Sopenharmony_ci</ul>
11451cb0ef41Sopenharmony_ci<p>Called when the promise receives a resolution or rejection value. This may
11461cb0ef41Sopenharmony_cioccur synchronously in the case of <code>Promise.resolve()</code> or <code>Promise.reject()</code>.</p>
11471cb0ef41Sopenharmony_ci</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>
11481cb0ef41Sopenharmony_ci<div class="api_metadata">
11491cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span>
11501cb0ef41Sopenharmony_ci</div>
11511cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
11521cb0ef41Sopenharmony_ci<p>The <code>v8.startupSnapshot</code> interface can be used to add serialization and
11531cb0ef41Sopenharmony_cideserialization hooks for custom startup snapshots.</p>
11541cb0ef41Sopenharmony_ci<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>
11551cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_"># </span><span class="language-bash">This launches a process with the snapshot</span>
11561cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">$ </span><span class="language-bash">node --snapshot-blob snapshot.blob</span></code> <button class="copy-button">copy</button></pre>
11571cb0ef41Sopenharmony_ci<p>In the example above, <code>entry.js</code> can use methods from the <code>v8.startupSnapshot</code>
11581cb0ef41Sopenharmony_ciinterface to specify how to save information for custom objects in the snapshot
11591cb0ef41Sopenharmony_ciduring serialization and how the information can be used to synchronize these
11601cb0ef41Sopenharmony_ciobjects during deserialization of the snapshot. For example, if the <code>entry.js</code>
11611cb0ef41Sopenharmony_cicontains the following script:</p>
11621cb0ef41Sopenharmony_ci<pre><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
11651cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:zlib'</span>);
11661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
11671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
11681cb0ef41Sopenharmony_ci
11691cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:v8'</span>);
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">BookShelf</span> {
11721cb0ef41Sopenharmony_ci  storage = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Reading a series of files from directory and store them into storage.</span>
11751cb0ef41Sopenharmony_ci  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">directory, books</span>) {
11761cb0ef41Sopenharmony_ci    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> book <span class="hljs-keyword">of</span> books) {
11771cb0ef41Sopenharmony_ci      <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)));
11781cb0ef41Sopenharmony_ci    }
11791cb0ef41Sopenharmony_ci  }
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ci  <span class="hljs-keyword">static</span> <span class="hljs-title function_">compressAll</span>(<span class="hljs-params">shelf</span>) {
11821cb0ef41Sopenharmony_ci    <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>) {
11831cb0ef41Sopenharmony_ci      shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gzipSync</span>(content));
11841cb0ef41Sopenharmony_ci    }
11851cb0ef41Sopenharmony_ci  }
11861cb0ef41Sopenharmony_ci
11871cb0ef41Sopenharmony_ci  <span class="hljs-keyword">static</span> <span class="hljs-title function_">decompressAll</span>(<span class="hljs-params">shelf</span>) {
11881cb0ef41Sopenharmony_ci    <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>) {
11891cb0ef41Sopenharmony_ci      shelf.<span class="hljs-property">storage</span>.<span class="hljs-title function_">set</span>(book, zlib.<span class="hljs-title function_">gunzipSync</span>(content));
11901cb0ef41Sopenharmony_ci    }
11911cb0ef41Sopenharmony_ci  }
11921cb0ef41Sopenharmony_ci}
11931cb0ef41Sopenharmony_ci
11941cb0ef41Sopenharmony_ci<span class="hljs-comment">// __dirname here is where the snapshot script is placed</span>
11951cb0ef41Sopenharmony_ci<span class="hljs-comment">// during snapshot building time.</span>
11961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> shelf = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BookShelf</span>(__dirname, [
11971cb0ef41Sopenharmony_ci  <span class="hljs-string">'book1.en_US.txt'</span>,
11981cb0ef41Sopenharmony_ci  <span class="hljs-string">'book1.es_ES.txt'</span>,
11991cb0ef41Sopenharmony_ci  <span class="hljs-string">'book2.zh_CN.txt'</span>,
12001cb0ef41Sopenharmony_ci]);
12011cb0ef41Sopenharmony_ci
12021cb0ef41Sopenharmony_ci<span class="hljs-title function_">assert</span>(v8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">isBuildingSnapshot</span>());
12031cb0ef41Sopenharmony_ci<span class="hljs-comment">// On snapshot serialization, compress the books to reduce size.</span>
12041cb0ef41Sopenharmony_civ8.<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);
12051cb0ef41Sopenharmony_ci<span class="hljs-comment">// On snapshot deserialization, decompress the books.</span>
12061cb0ef41Sopenharmony_civ8.<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);
12071cb0ef41Sopenharmony_civ8.<span class="hljs-property">startupSnapshot</span>.<span class="hljs-title function_">setDeserializeMainFunction</span>(<span class="hljs-function">(<span class="hljs-params">shelf</span>) =></span> {
12081cb0ef41Sopenharmony_ci  <span class="hljs-comment">// process.env and process.argv are refreshed during snapshot</span>
12091cb0ef41Sopenharmony_ci  <span class="hljs-comment">// deserialization.</span>
12101cb0ef41Sopenharmony_ci  <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>;
12111cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> book = process.<span class="hljs-property">argv</span>[<span class="hljs-number">1</span>];
12121cb0ef41Sopenharmony_ci  <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>;
12131cb0ef41Sopenharmony_ci  <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));
12141cb0ef41Sopenharmony_ci}, shelf);</code> <button class="copy-button">copy</button></pre>
12151cb0ef41Sopenharmony_ci<p>The resulted binary will get print the data deserialized from the snapshot
12161cb0ef41Sopenharmony_ciduring start up, using the refreshed <code>process.env</code> and <code>process.argv</code> of
12171cb0ef41Sopenharmony_cithe launched process:</p>
12181cb0ef41Sopenharmony_ci<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>
12191cb0ef41Sopenharmony_ci<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>
12201cb0ef41Sopenharmony_ci<p>Currently the application deserialized from a user-land snapshot cannot
12211cb0ef41Sopenharmony_cibe snapshotted again, so these APIs are only available to applications
12221cb0ef41Sopenharmony_cithat are not deserialized from a user-land snapshot.</p>
12231cb0ef41Sopenharmony_ci<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>
12241cb0ef41Sopenharmony_ci<div class="api_metadata">
12251cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span>
12261cb0ef41Sopenharmony_ci</div>
12271cb0ef41Sopenharmony_ci<ul>
12281cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback to be invoked before serialization.</li>
12291cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional data that will be passed to the <code>callback</code> when it
12301cb0ef41Sopenharmony_cigets called.</li>
12311cb0ef41Sopenharmony_ci</ul>
12321cb0ef41Sopenharmony_ci<p>Add a callback that will be called when the Node.js instance is about to
12331cb0ef41Sopenharmony_ciget serialized into a snapshot and exit. This can be used to release
12341cb0ef41Sopenharmony_ciresources that should not or cannot be serialized or to convert user data
12351cb0ef41Sopenharmony_ciinto a form more suitable for serialization.</p>
12361cb0ef41Sopenharmony_ci<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>
12371cb0ef41Sopenharmony_ci<div class="api_metadata">
12381cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span>
12391cb0ef41Sopenharmony_ci</div>
12401cb0ef41Sopenharmony_ci<ul>
12411cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback to be invoked after the snapshot is
12421cb0ef41Sopenharmony_cideserialized.</li>
12431cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional data that will be passed to the <code>callback</code> when it
12441cb0ef41Sopenharmony_cigets called.</li>
12451cb0ef41Sopenharmony_ci</ul>
12461cb0ef41Sopenharmony_ci<p>Add a callback that will be called when the Node.js instance is deserialized
12471cb0ef41Sopenharmony_cifrom a snapshot. The <code>callback</code> and the <code>data</code> (if provided) will be
12481cb0ef41Sopenharmony_ciserialized into the snapshot, they can be used to re-initialize the state
12491cb0ef41Sopenharmony_ciof the application or to re-acquire resources that the application needs
12501cb0ef41Sopenharmony_ciwhen the application is restarted from the snapshot.</p>
12511cb0ef41Sopenharmony_ci<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>
12521cb0ef41Sopenharmony_ci<div class="api_metadata">
12531cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span>
12541cb0ef41Sopenharmony_ci</div>
12551cb0ef41Sopenharmony_ci<ul>
12561cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback to be invoked as the entry point after the
12571cb0ef41Sopenharmony_cisnapshot is deserialized.</li>
12581cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional data that will be passed to the <code>callback</code> when it
12591cb0ef41Sopenharmony_cigets called.</li>
12601cb0ef41Sopenharmony_ci</ul>
12611cb0ef41Sopenharmony_ci<p>This sets the entry point of the Node.js application when it is deserialized
12621cb0ef41Sopenharmony_cifrom a snapshot. This can be called only once in the snapshot building
12631cb0ef41Sopenharmony_ciscript. If called, the deserialized application no longer needs an additional
12641cb0ef41Sopenharmony_cientry point script to start up and will simply invoke the callback along with
12651cb0ef41Sopenharmony_cithe deserialized data (if provided), otherwise an entry point script still
12661cb0ef41Sopenharmony_cineeds to be provided to the deserialized application.</p>
12671cb0ef41Sopenharmony_ci<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>
12681cb0ef41Sopenharmony_ci<div class="api_metadata">
12691cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span>
12701cb0ef41Sopenharmony_ci</div>
12711cb0ef41Sopenharmony_ci<ul>
12721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
12731cb0ef41Sopenharmony_ci</ul>
12741cb0ef41Sopenharmony_ci<p>Returns true if the Node.js instance is run to build a snapshot.</p>
12751cb0ef41Sopenharmony_ci</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>
12761cb0ef41Sopenharmony_ci<div class="api_metadata">
12771cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
12781cb0ef41Sopenharmony_ci</div>
12791cb0ef41Sopenharmony_ci<p>This API collects GC data in current thread.</p>
12801cb0ef41Sopenharmony_ci<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>
12811cb0ef41Sopenharmony_ci<div class="api_metadata">
12821cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
12831cb0ef41Sopenharmony_ci</div>
12841cb0ef41Sopenharmony_ci<p>Create a new instance of the <code>v8.GCProfiler</code> class.</p>
12851cb0ef41Sopenharmony_ci<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>
12861cb0ef41Sopenharmony_ci<div class="api_metadata">
12871cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
12881cb0ef41Sopenharmony_ci</div>
12891cb0ef41Sopenharmony_ci<p>Start collecting GC data.</p>
12901cb0ef41Sopenharmony_ci<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>
12911cb0ef41Sopenharmony_ci<div class="api_metadata">
12921cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
12931cb0ef41Sopenharmony_ci</div>
12941cb0ef41Sopenharmony_ci<p>Stop collecting GC data and return an object.The content of object
12951cb0ef41Sopenharmony_ciis as follows.</p>
12961cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">{</span>
12971cb0ef41Sopenharmony_ci  <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span>
12981cb0ef41Sopenharmony_ci  <span class="hljs-attr">"startTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059033862</span><span class="hljs-punctuation">,</span>
12991cb0ef41Sopenharmony_ci  <span class="hljs-attr">"statistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
13001cb0ef41Sopenharmony_ci    <span class="hljs-punctuation">{</span>
13011cb0ef41Sopenharmony_ci      <span class="hljs-attr">"gcType"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Scavenge"</span><span class="hljs-punctuation">,</span>
13021cb0ef41Sopenharmony_ci      <span class="hljs-attr">"beforeGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
13031cb0ef41Sopenharmony_ci        <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
13041cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5005312</span><span class="hljs-punctuation">,</span>
13051cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span>
13061cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5226496</span><span class="hljs-punctuation">,</span>
13071cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341325216</span><span class="hljs-punctuation">,</span>
13081cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span>
13091cb0ef41Sopenharmony_ci          <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span>
13101cb0ef41Sopenharmony_ci          <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4883840</span><span class="hljs-punctuation">,</span>
13111cb0ef41Sopenharmony_ci          <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span>
13121cb0ef41Sopenharmony_ci          <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span>
13131cb0ef41Sopenharmony_ci          <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span>
13141cb0ef41Sopenharmony_ci          <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span>
13151cb0ef41Sopenharmony_ci        <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
13161cb0ef41Sopenharmony_ci        <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
13171cb0ef41Sopenharmony_ci          <span class="hljs-punctuation">{</span>
13181cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span>
13191cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13201cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13211cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13221cb0ef41Sopenharmony_ci            <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span>
13231cb0ef41Sopenharmony_ci          <span class="hljs-punctuation">}</span>
13241cb0ef41Sopenharmony_ci        <span class="hljs-punctuation">]</span>
13251cb0ef41Sopenharmony_ci      <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
13261cb0ef41Sopenharmony_ci      <span class="hljs-attr">"cost"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1574.14</span><span class="hljs-punctuation">,</span>
13271cb0ef41Sopenharmony_ci      <span class="hljs-attr">"afterGC"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
13281cb0ef41Sopenharmony_ci        <span class="hljs-attr">"heapStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
13291cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">6053888</span><span class="hljs-punctuation">,</span>
13301cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalHeapSizeExecutable"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">524288</span><span class="hljs-punctuation">,</span>
13311cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalPhysicalSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5500928</span><span class="hljs-punctuation">,</span>
13321cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4341101384</span><span class="hljs-punctuation">,</span>
13331cb0ef41Sopenharmony_ci          <span class="hljs-attr">"totalGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">8192</span><span class="hljs-punctuation">,</span>
13341cb0ef41Sopenharmony_ci          <span class="hljs-attr">"usedGlobalHandlesSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2112</span><span class="hljs-punctuation">,</span>
13351cb0ef41Sopenharmony_ci          <span class="hljs-attr">"usedHeapSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4059096</span><span class="hljs-punctuation">,</span>
13361cb0ef41Sopenharmony_ci          <span class="hljs-attr">"heapSizeLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4345298944</span><span class="hljs-punctuation">,</span>
13371cb0ef41Sopenharmony_ci          <span class="hljs-attr">"mallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">254128</span><span class="hljs-punctuation">,</span>
13381cb0ef41Sopenharmony_ci          <span class="hljs-attr">"externalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">225138</span><span class="hljs-punctuation">,</span>
13391cb0ef41Sopenharmony_ci          <span class="hljs-attr">"peakMallocedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">181760</span>
13401cb0ef41Sopenharmony_ci        <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
13411cb0ef41Sopenharmony_ci        <span class="hljs-attr">"heapSpaceStatistics"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
13421cb0ef41Sopenharmony_ci          <span class="hljs-punctuation">{</span>
13431cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"read_only_space"</span><span class="hljs-punctuation">,</span>
13441cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13451cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceUsedSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13461cb0ef41Sopenharmony_ci            <span class="hljs-attr">"spaceAvailableSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
13471cb0ef41Sopenharmony_ci            <span class="hljs-attr">"physicalSpaceSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span>
13481cb0ef41Sopenharmony_ci          <span class="hljs-punctuation">}</span>
13491cb0ef41Sopenharmony_ci        <span class="hljs-punctuation">]</span>
13501cb0ef41Sopenharmony_ci      <span class="hljs-punctuation">}</span>
13511cb0ef41Sopenharmony_ci    <span class="hljs-punctuation">}</span>
13521cb0ef41Sopenharmony_ci  <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
13531cb0ef41Sopenharmony_ci  <span class="hljs-attr">"endTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1674059036865</span>
13541cb0ef41Sopenharmony_ci<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
13551cb0ef41Sopenharmony_ci<p>Here's an example.</p>
13561cb0ef41Sopenharmony_ci<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>);
13571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> profiler = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GCProfiler</span>();
13581cb0ef41Sopenharmony_ciprofiler.<span class="hljs-title function_">start</span>();
13591cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
13601cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(profiler.<span class="hljs-title function_">stop</span>());
13611cb0ef41Sopenharmony_ci}, <span class="hljs-number">1000</span>);</code> <button class="copy-button">copy</button></pre></section>
13621cb0ef41Sopenharmony_ci        <!-- API END -->
13631cb0ef41Sopenharmony_ci      </div>
13641cb0ef41Sopenharmony_ci    </div>
13651cb0ef41Sopenharmony_ci  </div>
13661cb0ef41Sopenharmony_ci</body>
13671cb0ef41Sopenharmony_ci</html>
1368