• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.20.1">
7  <title>V8 | Node.js v14.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/v8.html">
12</head>
13<body class="alt apidoc" id="api-section-v8">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8 active">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="v8" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.20.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="v8.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/v8.html">18.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v17.x/api/v8.html">17.x</a></li>
121<li><a href="https://nodejs.org/docs/latest-v16.x/api/v8.html">16.x <b>LTS</b></a></li>
122<li><a href="https://nodejs.org/docs/latest-v15.x/api/v8.html">15.x</a></li>
123<li><a href="https://nodejs.org/docs/latest-v14.x/api/v8.html">14.x <b>LTS</b></a></li>
124<li><a href="https://nodejs.org/docs/latest-v13.x/api/v8.html">13.x</a></li>
125<li><a href="https://nodejs.org/docs/latest-v12.x/api/v8.html">12.x <b>LTS</b></a></li>
126<li><a href="https://nodejs.org/docs/latest-v11.x/api/v8.html">11.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v10.x/api/v8.html">10.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v9.x/api/v8.html">9.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v8.x/api/v8.html">8.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v7.x/api/v8.html">7.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v6.x/api/v8.html">6.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v5.x/api/v8.html">5.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v4.x/api/v8.html">4.x</a></li></ol>
134    </li>
135
136            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/v8.md">Edit on GitHub</a></li>
137          </ul>
138        </div>
139        <hr>
140      </header>
141
142      <details id="toc" open><summary>Table of contents</summary><ul>
143<li><a href="#v8_v8">V8</a>
144<ul>
145<li><a href="#v8_v8_cacheddataversiontag"><code>v8.cachedDataVersionTag()</code></a></li>
146<li><a href="#v8_v8_getheapcodestatistics"><code>v8.getHeapCodeStatistics()</code></a></li>
147<li><a href="#v8_v8_getheapsnapshot"><code>v8.getHeapSnapshot()</code></a></li>
148<li><a href="#v8_v8_getheapspacestatistics"><code>v8.getHeapSpaceStatistics()</code></a></li>
149<li><a href="#v8_v8_getheapstatistics"><code>v8.getHeapStatistics()</code></a></li>
150<li><a href="#v8_v8_setflagsfromstring_flags"><code>v8.setFlagsFromString(flags)</code></a></li>
151<li><a href="#v8_v8_stopcoverage"><code>v8.stopCoverage()</code></a></li>
152<li><a href="#v8_v8_takecoverage"><code>v8.takeCoverage()</code></a></li>
153<li><a href="#v8_v8_writeheapsnapshot_filename"><code>v8.writeHeapSnapshot([filename])</code></a></li>
154<li><a href="#v8_serialization_api">Serialization API</a>
155<ul>
156<li><a href="#v8_v8_serialize_value"><code>v8.serialize(value)</code></a></li>
157<li><a href="#v8_v8_deserialize_buffer"><code>v8.deserialize(buffer)</code></a></li>
158<li><a href="#v8_class_v8_serializer">Class: <code>v8.Serializer</code></a>
159<ul>
160<li><a href="#v8_new_serializer"><code>new Serializer()</code></a></li>
161<li><a href="#v8_serializer_writeheader"><code>serializer.writeHeader()</code></a></li>
162<li><a href="#v8_serializer_writevalue_value"><code>serializer.writeValue(value)</code></a></li>
163<li><a href="#v8_serializer_releasebuffer"><code>serializer.releaseBuffer()</code></a></li>
164<li><a href="#v8_serializer_transferarraybuffer_id_arraybuffer"><code>serializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
165<li><a href="#v8_serializer_writeuint32_value"><code>serializer.writeUint32(value)</code></a></li>
166<li><a href="#v8_serializer_writeuint64_hi_lo"><code>serializer.writeUint64(hi, lo)</code></a></li>
167<li><a href="#v8_serializer_writedouble_value"><code>serializer.writeDouble(value)</code></a></li>
168<li><a href="#v8_serializer_writerawbytes_buffer"><code>serializer.writeRawBytes(buffer)</code></a></li>
169<li><a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject(object)</code></a></li>
170<li><a href="#v8_serializer_getdatacloneerror_message"><code>serializer._getDataCloneError(message)</code></a></li>
171<li><a href="#v8_serializer_getsharedarraybufferid_sharedarraybuffer"><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code></a></li>
172<li><a href="#v8_serializer_settreatarraybufferviewsashostobjects_flag"><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code></a></li>
173</ul>
174</li>
175<li><a href="#v8_class_v8_deserializer">Class: <code>v8.Deserializer</code></a>
176<ul>
177<li><a href="#v8_new_deserializer_buffer"><code>new Deserializer(buffer)</code></a></li>
178<li><a href="#v8_deserializer_readheader"><code>deserializer.readHeader()</code></a></li>
179<li><a href="#v8_deserializer_readvalue"><code>deserializer.readValue()</code></a></li>
180<li><a href="#v8_deserializer_transferarraybuffer_id_arraybuffer"><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code></a></li>
181<li><a href="#v8_deserializer_getwireformatversion"><code>deserializer.getWireFormatVersion()</code></a></li>
182<li><a href="#v8_deserializer_readuint32"><code>deserializer.readUint32()</code></a></li>
183<li><a href="#v8_deserializer_readuint64"><code>deserializer.readUint64()</code></a></li>
184<li><a href="#v8_deserializer_readdouble"><code>deserializer.readDouble()</code></a></li>
185<li><a href="#v8_deserializer_readrawbytes_length"><code>deserializer.readRawBytes(length)</code></a></li>
186<li><a href="#v8_deserializer_readhostobject"><code>deserializer._readHostObject()</code></a></li>
187</ul>
188</li>
189<li><a href="#v8_class_v8_defaultserializer">Class: <code>v8.DefaultSerializer</code></a></li>
190<li><a href="#v8_class_v8_defaultdeserializer">Class: <code>v8.DefaultDeserializer</code></a></li>
191</ul>
192</li>
193</ul>
194</li>
195</ul></details>
196
197      <div id="apicontent">
198        <h2>V8<span><a class="mark" href="#v8_v8" id="v8_v8">#</a></span></h2>
199
200<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/v8.js">lib/v8.js</a></p>
201<p>The <code>v8</code> module exposes APIs that are specific to the version of <a href="https://developers.google.com/v8/">V8</a>
202built into the Node.js binary. It can be accessed using:</p>
203<pre><code class="language-js"><span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'v8'</span>);</code></pre>
204<section><h3><code>v8.cachedDataVersionTag()</code><span><a class="mark" href="#v8_v8_cacheddataversiontag" id="v8_v8_cacheddataversiontag">#</a></span></h3>
205<div class="api_metadata">
206<span>Added in: v8.0.0</span>
207</div>
208<ul>
209<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
210</ul>
211<p>Returns an integer representing a version tag derived from the V8 version,
212command-line flags, and detected CPU features. This is useful for determining
213whether a <a href="vm.html#vm_new_vm_script_code_options"><code>vm.Script</code></a> <code>cachedData</code> buffer is compatible with this instance
214of V8.</p>
215<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>
216<span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V8</span>
217<span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the value</span>
218<span class="hljs-comment">// does indeed update when flags are toggled.</span>
219v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_natives_syntax'</span>);
220<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></pre>
221</section><section><h3><code>v8.getHeapCodeStatistics()</code><span><a class="mark" href="#v8_v8_getheapcodestatistics" id="v8_v8_getheapcodestatistics">#</a></span></h3>
222<div class="api_metadata">
223<span>Added in: v12.8.0</span>
224</div>
225<ul>
226<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
227</ul>
228<p>Returns an object with the following properties:</p>
229<ul>
230<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>
231<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>
232<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>
233</ul>
234<!-- eslint-skip -->
235<pre><code class="language-js">{
236  <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>,
237  <span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>,
238  <span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</span>
239}</code></pre>
240</section><section><h3><code>v8.getHeapSnapshot()</code><span><a class="mark" href="#v8_v8_getheapsnapshot" id="v8_v8_getheapsnapshot">#</a></span></h3>
241<div class="api_metadata">
242<span>Added in: v11.13.0</span>
243</div>
244<ul>
245<li>Returns: <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a> A Readable Stream containing the V8 heap snapshot</li>
246</ul>
247<p>Generates a snapshot of the current V8 heap and returns a Readable
248Stream that may be used to read the JSON serialized representation.
249This JSON stream format is intended to be used with tools such as
250Chrome DevTools. The JSON schema is undocumented and specific to the
251V8 engine. Therefore, the schema may change from one version of V8 to the next.</p>
252<pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</span>
253<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'v8'</span>);
254<span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnapshot</span>();
255stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code></pre>
256</section><section><h3><code>v8.getHeapSpaceStatistics()</code><span><a class="mark" href="#v8_v8_getheapspacestatistics" id="v8_v8_getheapspacestatistics">#</a></span></h3>
257<div class="api_metadata">
258<details class="changelog"><summary>History</summary>
259<table>
260<tbody><tr><th>Version</th><th>Changes</th></tr>
261<tr><td>v7.5.0</td>
262<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
263<tr><td>v6.0.0</td>
264<td><p><span>Added in: v6.0.0</span></p></td></tr>
265</tbody></table>
266</details>
267</div>
268<ul>
269<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a></li>
270</ul>
271<p>Returns statistics about the V8 heap spaces, i.e. the segments which make up
272the V8 heap. Neither the ordering of heap spaces, nor the availability of a
273heap space can be guaranteed as the statistics are provided via the V8
274<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
275next.</p>
276<p>The value returned is an array of objects containing the following properties:</p>
277<ul>
278<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>
279<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>
280<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>
281<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>
282<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>
283</ul>
284<pre><code class="language-json"><span class="hljs-punctuation">[</span>
285  <span class="hljs-punctuation">{</span>
286    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"new_space"</span><span class="hljs-punctuation">,</span>
287    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span><span class="hljs-punctuation">,</span>
288    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">951112</span><span class="hljs-punctuation">,</span>
289    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">80824</span><span class="hljs-punctuation">,</span>
290    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2063872</span>
291  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
292  <span class="hljs-punctuation">{</span>
293    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"old_space"</span><span class="hljs-punctuation">,</span>
294    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span><span class="hljs-punctuation">,</span>
295    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2493792</span><span class="hljs-punctuation">,</span>
296    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
297    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3090560</span>
298  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
299  <span class="hljs-punctuation">{</span>
300    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"code_space"</span><span class="hljs-punctuation">,</span>
301    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span><span class="hljs-punctuation">,</span>
302    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">644256</span><span class="hljs-punctuation">,</span>
303    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">960</span><span class="hljs-punctuation">,</span>
304    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1260160</span>
305  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
306  <span class="hljs-punctuation">{</span>
307    <span class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"map_space"</span><span class="hljs-punctuation">,</span>
308    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span><span class="hljs-punctuation">,</span>
309    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">201608</span><span class="hljs-punctuation">,</span>
310    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
311    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1094160</span>
312  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
313  <span class="hljs-punctuation">{</span>
314    <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>
315    <span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
316    <span class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>
317    <span class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1490980608</span><span class="hljs-punctuation">,</span>
318    <span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span>
319  <span class="hljs-punctuation">}</span>
320<span class="hljs-punctuation">]</span></code></pre>
321</section><section><h3><code>v8.getHeapStatistics()</code><span><a class="mark" href="#v8_v8_getheapstatistics" id="v8_v8_getheapstatistics">#</a></span></h3>
322<div class="api_metadata">
323<details class="changelog"><summary>History</summary>
324<table>
325<tbody><tr><th>Version</th><th>Changes</th></tr>
326<tr><td>v7.5.0</td>
327<td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
328<tr><td>v7.2.0</td>
329<td><p>Added <code>malloced_memory</code>, <code>peak_malloced_memory</code>, and <code>does_zap_garbage</code>.</p></td></tr>
330<tr><td>v1.0.0</td>
331<td><p><span>Added in: v1.0.0</span></p></td></tr>
332</tbody></table>
333</details>
334</div>
335<ul>
336<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
337</ul>
338<p>Returns an object with the following properties:</p>
339<ul>
340<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>
341<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>
342<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>
343<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>
344<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>
345<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>
346<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>
347<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>
348<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>
349<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>
350<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>
351</ul>
352<p><code>does_zap_garbage</code> is a 0/1 boolean, which signifies whether the
353<code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap
354garbage with a bit pattern. The RSS footprint (resident set size) gets bigger
355because it continuously touches all heap pages and that makes them less likely
356to get swapped out by the operating system.</p>
357<p><code>number_of_native_contexts</code> The value of native_context is the number of the
358top-level contexts currently active. Increase of this number over time indicates
359a memory leak.</p>
360<p><code>number_of_detached_contexts</code> The value of detached_context is the number
361of contexts that were detached and not yet garbage collected. This number
362being non-zero indicates a potential memory leak.</p>
363<!-- eslint-skip -->
364<pre><code class="language-js">{
365  <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>,
366  <span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span>,
367  <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>,
368  <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>,
369  <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>,
370  <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>,
371  <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>,
372  <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>,
373  <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>,
374  <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>,
375  <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>
376}</code></pre>
377</section><section><h3><code>v8.setFlagsFromString(flags)</code><span><a class="mark" href="#v8_v8_setflagsfromstring_flags" id="v8_v8_setflagsfromstring_flags">#</a></span></h3>
378<div class="api_metadata">
379<span>Added in: v1.0.0</span>
380</div>
381<ul>
382<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>
383</ul>
384<p>The <code>v8.setFlagsFromString()</code> method can be used to programmatically set
385V8 command-line flags. This method should be used with care. Changing settings
386after the VM has started may result in unpredictable behavior, including
387crashes and data loss; or it may simply do nothing.</p>
388<p>The V8 options available for a version of Node.js may be determined by running
389<code>node --v8-options</code>.</p>
390<p>Usage:</p>
391<pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minute.</span>
392<span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'v8'</span>);
393v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_gc'</span>);
394<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></pre>
395</section><section><h3><code>v8.stopCoverage()</code><span><a class="mark" href="#v8_v8_stopcoverage" id="v8_v8_stopcoverage">#</a></span></h3>
396<div class="api_metadata">
397<span>Added in: v14.18.0</span>
398</div>
399<p>The <code>v8.stopCoverage()</code> method allows the user to stop the coverage collection
400started by <a href="cli.html#cli_node_v8_coverage_dir"><code>NODE_V8_COVERAGE</code></a>, so that V8 can release the execution count
401records and optimize code. This can be used in conjunction with
402<a href="#v8_v8_takecoverage"><code>v8.takeCoverage()</code></a> if the user wants to collect the coverage on demand.</p>
403</section><section><h3><code>v8.takeCoverage()</code><span><a class="mark" href="#v8_v8_takecoverage" id="v8_v8_takecoverage">#</a></span></h3>
404<div class="api_metadata">
405<span>Added in: v14.18.0</span>
406</div>
407<p>The <code>v8.takeCoverage()</code> method allows the user to write the coverage started by
408<a href="cli.html#cli_node_v8_coverage_dir"><code>NODE_V8_COVERAGE</code></a> to disk on demand. This method can be invoked multiple
409times during the lifetime of the process. Each time the execution counter will
410be reset and a new coverage report will be written to the directory specified
411by <a href="cli.html#cli_node_v8_coverage_dir"><code>NODE_V8_COVERAGE</code></a>.</p>
412<p>When the process is about to exit, one last coverage will still be written to
413disk unless <a href="#v8_v8_stopcoverage"><code>v8.stopCoverage()</code></a> is invoked before the process exits.</p>
414</section><section><h3><code>v8.writeHeapSnapshot([filename])</code><span><a class="mark" href="#v8_v8_writeheapsnapshot_filename" id="v8_v8_writeheapsnapshot_filename">#</a></span></h3>
415<div class="api_metadata">
416<span>Added in: v11.13.0</span>
417</div>
418<ul>
419<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
420saved. If not specified, a file name with the pattern
421<code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be
422generated, where <code>{pid}</code> will be the PID of the Node.js process,
423<code>{thread_id}</code> will be <code>0</code> when <code>writeHeapSnapshot()</code> is called from
424the main Node.js thread or the id of a worker thread.</li>
425<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>
426</ul>
427<p>Generates a snapshot of the current V8 heap and writes it to a JSON
428file. This file is intended to be used with tools such as Chrome
429DevTools. The JSON schema is undocumented and specific to the V8
430engine, and may change from one version of V8 to the next.</p>
431<p>A heap snapshot is specific to a single V8 isolate. When using
432<a href="worker_threads.html">worker threads</a>, a heap snapshot generated from the main thread will
433not contain any information about the workers, and vice versa.</p>
434<pre><code class="language-js"><span class="hljs-keyword">const</span> { writeHeapSnapshot } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'v8'</span>);
435<span class="hljs-keyword">const</span> {
436  <span class="hljs-title class_">Worker</span>,
437  isMainThread,
438  parentPort
439} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
440
441<span class="hljs-keyword">if</span> (isMainThread) {
442  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
443
444  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> {
445    <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>);
446    <span class="hljs-comment">// Now get a heapdump for the main thread.</span>
447    <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>);
448  });
449
450  <span class="hljs-comment">// Tell the worker to create a heapdump.</span>
451  worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'</span>);
452} <span class="hljs-keyword">else</span> {
453  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> {
454    <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) {
455      <span class="hljs-comment">// Generate a heapdump for the worker</span>
456      <span class="hljs-comment">// and return the filename to the parent.</span>
457      parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">writeHeapSnapshot</span>());
458    }
459  });
460}</code></pre>
461</section><section><h3>Serialization API<span><a class="mark" href="#v8_serialization_api" id="v8_serialization_api">#</a></span></h3>
462<p>The serialization API provides means of serializing JavaScript values in a way
463that 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>
464<p>The format is backward-compatible (i.e. safe to store to disk).
465Equal JavaScript values may result in different serialized output.</p>
466<h4><code>v8.serialize(value)</code><span><a class="mark" href="#v8_v8_serialize_value" id="v8_v8_serialize_value">#</a></span></h4>
467<div class="api_metadata">
468<span>Added in: v8.0.0</span>
469</div>
470<ul>
471<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>
472<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
473</ul>
474<p>Uses a <a href="#v8_class_v8_defaultserializer"><code>DefaultSerializer</code></a> to serialize <code>value</code> into a buffer.</p>
475<h4><code>v8.deserialize(buffer)</code><span><a class="mark" href="#v8_v8_deserialize_buffer" id="v8_v8_deserialize_buffer">#</a></span></h4>
476<div class="api_metadata">
477<span>Added in: v8.0.0</span>
478</div>
479<ul>
480<li><code>buffer</code> <a href="buffer.html#buffer_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="#v8_v8_serialize_value"><code>serialize()</code></a>.</li>
481</ul>
482<p>Uses a <a href="#v8_class_v8_defaultdeserializer"><code>DefaultDeserializer</code></a> with default options to read a JS value
483from a buffer.</p>
484<h4>Class: <code>v8.Serializer</code><span><a class="mark" href="#v8_class_v8_serializer" id="v8_class_v8_serializer">#</a></span></h4>
485<div class="api_metadata">
486<span>Added in: v8.0.0</span>
487</div>
488<h5><code>new Serializer()</code><span><a class="mark" href="#v8_new_serializer" id="v8_new_serializer">#</a></span></h5>
489<p>Creates a new <code>Serializer</code> object.</p>
490<h5><code>serializer.writeHeader()</code><span><a class="mark" href="#v8_serializer_writeheader" id="v8_serializer_writeheader">#</a></span></h5>
491<p>Writes out a header, which includes the serialization format version.</p>
492<h5><code>serializer.writeValue(value)</code><span><a class="mark" href="#v8_serializer_writevalue_value" id="v8_serializer_writevalue_value">#</a></span></h5>
493<ul>
494<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>
495</ul>
496<p>Serializes a JavaScript value and adds the serialized representation to the
497internal buffer.</p>
498<p>This throws an error if <code>value</code> cannot be serialized.</p>
499<h5><code>serializer.releaseBuffer()</code><span><a class="mark" href="#v8_serializer_releasebuffer" id="v8_serializer_releasebuffer">#</a></span></h5>
500<ul>
501<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
502</ul>
503<p>Returns the stored internal buffer. This serializer should not be used once
504the buffer is released. Calling this method results in undefined behavior
505if a previous write has failed.</p>
506<h5><code>serializer.transferArrayBuffer(id, arrayBuffer)</code><span><a class="mark" href="#v8_serializer_transferarraybuffer_id_arraybuffer" id="v8_serializer_transferarraybuffer_id_arraybuffer">#</a></span></h5>
507<ul>
508<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>
509<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>
510</ul>
511<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band.
512Pass the corresponding <code>ArrayBuffer</code> in the deserializing context to
513<a href="#v8_deserializer_transferarraybuffer_id_arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p>
514<h5><code>serializer.writeUint32(value)</code><span><a class="mark" href="#v8_serializer_writeuint32_value" id="v8_serializer_writeuint32_value">#</a></span></h5>
515<ul>
516<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>
517</ul>
518<p>Write a raw 32-bit unsigned integer.
519For use inside of a custom <a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject()</code></a>.</p>
520<h5><code>serializer.writeUint64(hi, lo)</code><span><a class="mark" href="#v8_serializer_writeuint64_hi_lo" id="v8_serializer_writeuint64_hi_lo">#</a></span></h5>
521<ul>
522<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>
523<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>
524</ul>
525<p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
526For use inside of a custom <a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject()</code></a>.</p>
527<h5><code>serializer.writeDouble(value)</code><span><a class="mark" href="#v8_serializer_writedouble_value" id="v8_serializer_writedouble_value">#</a></span></h5>
528<ul>
529<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>
530</ul>
531<p>Write a JS <code>number</code> value.
532For use inside of a custom <a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject()</code></a>.</p>
533<h5><code>serializer.writeRawBytes(buffer)</code><span><a class="mark" href="#v8_serializer_writerawbytes_buffer" id="v8_serializer_writerawbytes_buffer">#</a></span></h5>
534<ul>
535<li><code>buffer</code> <a href="buffer.html#buffer_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>
536</ul>
537<p>Write raw bytes into the serializer’s internal buffer. The deserializer
538will require a way to compute the length of the buffer.
539For use inside of a custom <a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject()</code></a>.</p>
540<h5><code>serializer._writeHostObject(object)</code><span><a class="mark" href="#v8_serializer_writehostobject_object" id="v8_serializer_writehostobject_object">#</a></span></h5>
541<ul>
542<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>
543</ul>
544<p>This method is called to write some kind of host object, i.e. an object created
545by native C++ bindings. If it is not possible to serialize <code>object</code>, a suitable
546exception should be thrown.</p>
547<p>This method is not present on the <code>Serializer</code> class itself but can be provided
548by subclasses.</p>
549<h5><code>serializer._getDataCloneError(message)</code><span><a class="mark" href="#v8_serializer_getdatacloneerror_message" id="v8_serializer_getdatacloneerror_message">#</a></span></h5>
550<ul>
551<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>
552</ul>
553<p>This method is called to generate error objects that will be thrown when an
554object can not be cloned.</p>
555<p>This method defaults to the <a href="errors.html#errors_class_error"><code>Error</code></a> constructor and can be overridden on
556subclasses.</p>
557<h5><code>serializer._getSharedArrayBufferId(sharedArrayBuffer)</code><span><a class="mark" href="#v8_serializer_getsharedarraybufferid_sharedarraybuffer" id="v8_serializer_getsharedarraybufferid_sharedarraybuffer">#</a></span></h5>
558<ul>
559<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>
560</ul>
561<p>This method is called when the serializer is going to serialize a
562<code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for
563the object, using the same ID if this <code>SharedArrayBuffer</code> has already been
564serialized. When deserializing, this ID will be passed to
565<a href="#v8_deserializer_transferarraybuffer_id_arraybuffer"><code>deserializer.transferArrayBuffer()</code></a>.</p>
566<p>If the object cannot be serialized, an exception should be thrown.</p>
567<p>This method is not present on the <code>Serializer</code> class itself but can be provided
568by subclasses.</p>
569<h5><code>serializer._setTreatArrayBufferViewsAsHostObjects(flag)</code><span><a class="mark" href="#v8_serializer_settreatarraybufferviewsashostobjects_flag" id="v8_serializer_settreatarraybufferviewsashostobjects_flag">#</a></span></h5>
570<ul>
571<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>
572</ul>
573<p>Indicate whether to treat <code>TypedArray</code> and <code>DataView</code> objects as
574host objects, i.e. pass them to <a href="#v8_serializer_writehostobject_object"><code>serializer._writeHostObject()</code></a>.</p>
575<h4>Class: <code>v8.Deserializer</code><span><a class="mark" href="#v8_class_v8_deserializer" id="v8_class_v8_deserializer">#</a></span></h4>
576<div class="api_metadata">
577<span>Added in: v8.0.0</span>
578</div>
579<h5><code>new Deserializer(buffer)</code><span><a class="mark" href="#v8_new_deserializer_buffer" id="v8_new_deserializer_buffer">#</a></span></h5>
580<ul>
581<li><code>buffer</code> <a href="buffer.html#buffer_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
582<a href="#v8_serializer_releasebuffer"><code>serializer.releaseBuffer()</code></a>.</li>
583</ul>
584<p>Creates a new <code>Deserializer</code> object.</p>
585<h5><code>deserializer.readHeader()</code><span><a class="mark" href="#v8_deserializer_readheader" id="v8_deserializer_readheader">#</a></span></h5>
586<p>Reads and validates a header (including the format version).
587May, for example, reject an invalid or unsupported wire format. In that case,
588an <code>Error</code> is thrown.</p>
589<h5><code>deserializer.readValue()</code><span><a class="mark" href="#v8_deserializer_readvalue" id="v8_deserializer_readvalue">#</a></span></h5>
590<p>Deserializes a JavaScript value from the buffer and returns it.</p>
591<h5><code>deserializer.transferArrayBuffer(id, arrayBuffer)</code><span><a class="mark" href="#v8_deserializer_transferarraybuffer_id_arraybuffer" id="v8_deserializer_transferarraybuffer_id_arraybuffer">#</a></span></h5>
592<ul>
593<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>
594<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>
595</ul>
596<p>Marks an <code>ArrayBuffer</code> as having its contents transferred out of band.
597Pass the corresponding <code>ArrayBuffer</code> in the serializing context to
598<a href="#v8_serializer_transferarraybuffer_id_arraybuffer"><code>serializer.transferArrayBuffer()</code></a> (or return the <code>id</code> from
599<a href="#v8_serializer_getsharedarraybufferid_sharedarraybuffer"><code>serializer._getSharedArrayBufferId()</code></a> in the case of <code>SharedArrayBuffer</code>s).</p>
600<h5><code>deserializer.getWireFormatVersion()</code><span><a class="mark" href="#v8_deserializer_getwireformatversion" id="v8_deserializer_getwireformatversion">#</a></span></h5>
601<ul>
602<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
603</ul>
604<p>Reads the underlying wire format version. Likely mostly to be useful to
605legacy code reading old wire format versions. May not be called before
606<code>.readHeader()</code>.</p>
607<h5><code>deserializer.readUint32()</code><span><a class="mark" href="#v8_deserializer_readuint32" id="v8_deserializer_readuint32">#</a></span></h5>
608<ul>
609<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
610</ul>
611<p>Read a raw 32-bit unsigned integer and return it.
612For use inside of a custom <a href="#v8_deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
613<h5><code>deserializer.readUint64()</code><span><a class="mark" href="#v8_deserializer_readuint64" id="v8_deserializer_readuint64">#</a></span></h5>
614<ul>
615<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer[]></a></li>
616</ul>
617<p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code>
618with two 32-bit unsigned integer entries.
619For use inside of a custom <a href="#v8_deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
620<h5><code>deserializer.readDouble()</code><span><a class="mark" href="#v8_deserializer_readdouble" id="v8_deserializer_readdouble">#</a></span></h5>
621<ul>
622<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
623</ul>
624<p>Read a JS <code>number</code> value.
625For use inside of a custom <a href="#v8_deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
626<h5><code>deserializer.readRawBytes(length)</code><span><a class="mark" href="#v8_deserializer_readrawbytes_length" id="v8_deserializer_readrawbytes_length">#</a></span></h5>
627<ul>
628<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>
629<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
630</ul>
631<p>Read raw bytes from the deserializer’s internal buffer. The <code>length</code> parameter
632must correspond to the length of the buffer that was passed to
633<a href="#v8_serializer_writerawbytes_buffer"><code>serializer.writeRawBytes()</code></a>.
634For use inside of a custom <a href="#v8_deserializer_readhostobject"><code>deserializer._readHostObject()</code></a>.</p>
635<h5><code>deserializer._readHostObject()</code><span><a class="mark" href="#v8_deserializer_readhostobject" id="v8_deserializer_readhostobject">#</a></span></h5>
636<p>This method is called to read some kind of host object, i.e. an object that is
637created by native C++ bindings. If it is not possible to deserialize the data,
638a suitable exception should be thrown.</p>
639<p>This method is not present on the <code>Deserializer</code> class itself but can be
640provided by subclasses.</p>
641<h4>Class: <code>v8.DefaultSerializer</code><span><a class="mark" href="#v8_class_v8_defaultserializer" id="v8_class_v8_defaultserializer">#</a></span></h4>
642<div class="api_metadata">
643<span>Added in: v8.0.0</span>
644</div>
645<p>A subclass of <a href="#v8_class_v8_serializer"><code>Serializer</code></a> that serializes <code>TypedArray</code>
646(in particular <a href="buffer.html"><code>Buffer</code></a>) and <code>DataView</code> objects as host objects, and only
647stores the part of their underlying <code>ArrayBuffer</code>s that they are referring to.</p>
648<h4>Class: <code>v8.DefaultDeserializer</code><span><a class="mark" href="#v8_class_v8_defaultdeserializer" id="v8_class_v8_defaultdeserializer">#</a></span></h4>
649<div class="api_metadata">
650<span>Added in: v8.0.0</span>
651</div>
652<p>A subclass of <a href="#v8_class_v8_deserializer"><code>Deserializer</code></a> corresponding to the format written by
653<a href="#v8_class_v8_defaultserializer"><code>DefaultSerializer</code></a>.</p></section>
654        <!-- API END -->
655      </div>
656    </div>
657  </div>
658  <script>
659    'use strict';
660    {
661      const kCustomPreference = 'customDarkTheme';
662      const userSettings = sessionStorage.getItem(kCustomPreference);
663      const themeToggleButton = document.getElementById('theme-toggle-btn');
664      if (userSettings === null && window.matchMedia) {
665        const mq = window.matchMedia('(prefers-color-scheme: dark)');
666        if ('onchange' in mq) {
667          function mqChangeListener(e) {
668            document.body.classList.toggle('dark-mode', e.matches);
669          }
670          mq.addEventListener('change', mqChangeListener);
671          if (themeToggleButton) {
672            themeToggleButton.addEventListener('click', function() {
673              mq.removeEventListener('change', mqChangeListener);
674            }, { once: true });
675          }
676        }
677        if (mq.matches) {
678          document.body.classList.add('dark-mode');
679        }
680      } else if (userSettings === 'true') {
681        document.body.classList.add('dark-mode');
682      }
683      if (themeToggleButton) {
684        themeToggleButton.hidden = false;
685        themeToggleButton.addEventListener('click', function() {
686          sessionStorage.setItem(
687            kCustomPreference,
688            document.body.classList.toggle('dark-mode')
689          );
690        });
691      }
692    }
693  </script>
694</body>
695</html>
696