{ "type": "module", "source": "doc/api/v8.md", "modules": [ { "textRaw": "V8", "name": "v8", "introduced_in": "v4.0.0", "desc": "

Source Code: lib/v8.js

\n

The v8 module exposes APIs that are specific to the version of V8\nbuilt into the Node.js binary. It can be accessed using:

\n
const v8 = require('v8');\n
\n

The APIs and implementation are subject to change at any time.

", "methods": [ { "textRaw": "`v8.cachedDataVersionTag()`", "type": "method", "name": "cachedDataVersionTag", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [] } ], "desc": "

Returns an integer representing a \"version tag\" derived from the V8 version,\ncommand line flags and detected CPU features. This is useful for determining\nwhether a vm.Script cachedData buffer is compatible with this instance\nof V8.

" }, { "textRaw": "`v8.getHeapSpaceStatistics()`", "type": "method", "name": "getHeapSpaceStatistics", "meta": { "added": [ "v6.0.0" ], "changes": [ { "version": "v7.5.0", "pr-url": "https://github.com/nodejs/node/pull/10186", "description": "Support values exceeding the 32-bit unsigned integer range." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Object[]}", "name": "return", "type": "Object[]" }, "params": [] } ], "desc": "

Returns statistics about the V8 heap spaces, i.e. the segments which make up\nthe V8 heap. Neither the ordering of heap spaces, nor the availability of a\nheap space can be guaranteed as the statistics are provided via the V8\nGetHeapSpaceStatistics function and may change from one V8 version to the\nnext.

\n

The value returned is an array of objects containing the following properties:

\n\n
[\n  {\n    \"space_name\": \"new_space\",\n    \"space_size\": 2063872,\n    \"space_used_size\": 951112,\n    \"space_available_size\": 80824,\n    \"physical_space_size\": 2063872\n  },\n  {\n    \"space_name\": \"old_space\",\n    \"space_size\": 3090560,\n    \"space_used_size\": 2493792,\n    \"space_available_size\": 0,\n    \"physical_space_size\": 3090560\n  },\n  {\n    \"space_name\": \"code_space\",\n    \"space_size\": 1260160,\n    \"space_used_size\": 644256,\n    \"space_available_size\": 960,\n    \"physical_space_size\": 1260160\n  },\n  {\n    \"space_name\": \"map_space\",\n    \"space_size\": 1094160,\n    \"space_used_size\": 201608,\n    \"space_available_size\": 0,\n    \"physical_space_size\": 1094160\n  },\n  {\n    \"space_name\": \"large_object_space\",\n    \"space_size\": 0,\n    \"space_used_size\": 0,\n    \"space_available_size\": 1490980608,\n    \"physical_space_size\": 0\n  }\n]\n
" }, { "textRaw": "`v8.getHeapSnapshot()`", "type": "method", "name": "getHeapSnapshot", "meta": { "added": [ "v11.13.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {stream.Readable} A Readable Stream containing the V8 heap snapshot", "name": "return", "type": "stream.Readable", "desc": "A Readable Stream containing the V8 heap snapshot" }, "params": [] } ], "desc": "

Generates a snapshot of the current V8 heap and returns a Readable\nStream that may be used to read the JSON serialized representation.\nThis JSON stream format is intended to be used with tools such as\nChrome DevTools. The JSON schema is undocumented and specific to the\nV8 engine, and may change from one version of V8 to the next.

\n
const stream = v8.getHeapSnapshot();\nstream.pipe(process.stdout);\n
" }, { "textRaw": "`v8.getHeapStatistics()`", "type": "method", "name": "getHeapStatistics", "meta": { "added": [ "v1.0.0" ], "changes": [ { "version": "v7.2.0", "pr-url": "https://github.com/nodejs/node/pull/8610", "description": "Added `malloced_memory`, `peak_malloced_memory`, and `does_zap_garbage`." }, { "version": "v7.5.0", "pr-url": "https://github.com/nodejs/node/pull/10186", "description": "Support values exceeding the 32-bit unsigned integer range." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [] } ], "desc": "

Returns an object with the following properties:

\n\n

does_zap_garbage is a 0/1 boolean, which signifies whether the\n--zap_code_space option is enabled or not. This makes V8 overwrite heap\ngarbage with a bit pattern. The RSS footprint (resident memory set) gets bigger\nbecause it continuously touches all heap pages and that makes them less likely\nto get swapped out by the operating system.

\n

number_of_native_contexts The value of native_context is the number of the\ntop-level contexts currently active. Increase of this number over time indicates\na memory leak.

\n

number_of_detached_contexts The value of detached_context is the number\nof contexts that were detached and not yet garbage collected. This number\nbeing non-zero indicates a potential memory leak.

\n\n
{\n  total_heap_size: 7326976,\n  total_heap_size_executable: 4194304,\n  total_physical_size: 7326976,\n  total_available_size: 1152656,\n  used_heap_size: 3476208,\n  heap_size_limit: 1535115264,\n  malloced_memory: 16384,\n  peak_malloced_memory: 1127496,\n  does_zap_garbage: 0,\n  number_of_native_contexts: 1,\n  number_of_detached_contexts: 0\n}\n
" }, { "textRaw": "`v8.getHeapCodeStatistics()`", "type": "method", "name": "getHeapCodeStatistics", "meta": { "added": [ "v12.8.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [] } ], "desc": "

Returns an object with the following properties:

\n\n\n
{\n  code_and_metadata_size: 212208,\n  bytecode_and_metadata_size: 161368,\n  external_script_source_size: 1410794\n}\n
" }, { "textRaw": "`v8.setFlagsFromString(flags)`", "type": "method", "name": "setFlagsFromString", "meta": { "added": [ "v1.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`flags` {string}", "name": "flags", "type": "string" } ] } ], "desc": "

The v8.setFlagsFromString() method can be used to programmatically set\nV8 command line flags. This method should be used with care. Changing settings\nafter the VM has started may result in unpredictable behavior, including\ncrashes and data loss; or it may simply do nothing.

\n

The V8 options available for a version of Node.js may be determined by running\nnode --v8-options.

\n

Usage:

\n
// Print GC events to stdout for one minute.\nconst v8 = require('v8');\nv8.setFlagsFromString('--trace_gc');\nsetTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3);\n
" }, { "textRaw": "`v8.takeCoverage()`", "type": "method", "name": "takeCoverage", "meta": { "added": [ "v12.22.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

The v8.takeCoverage() method allows the user to write the coverage started by\nNODE_V8_COVERAGE to disk on demand. This method can be invoked multiple\ntimes during the lifetime of the process, each time the execution counter will\nbe reset and a new coverage report will be written to the directory specified\nby NODE_V8_COVERAGE.

\n

When the process is about to exit, one last coverage will still be written to\ndisk, unless v8.stopCoverage() is invoked before the process exits.

" }, { "textRaw": "`v8.stopCoverage()`", "type": "method", "name": "stopCoverage", "meta": { "added": [ "v12.22.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

The v8.stopCoverage() method allows the user to stop the coverage collection\nstarted by NODE_V8_COVERAGE, so that V8 can release the execution count\nrecords and optimize code. This can be used in conjunction with\nv8.takeCoverage() if the user wants to collect the coverage on demand.

" }, { "textRaw": "`v8.writeHeapSnapshot([filename])`", "type": "method", "name": "writeHeapSnapshot", "meta": { "added": [ "v11.13.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string} The filename where the snapshot was saved.", "name": "return", "type": "string", "desc": "The filename where the snapshot was saved." }, "params": [ { "textRaw": "`filename` {string} The file path where the V8 heap snapshot is to be saved. If not specified, a file name with the pattern `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` will be generated, where `{pid}` will be the PID of the Node.js process, `{thread_id}` will be `0` when `writeHeapSnapshot()` is called from the main Node.js thread or the id of a worker thread.", "name": "filename", "type": "string", "desc": "The file path where the V8 heap snapshot is to be saved. If not specified, a file name with the pattern `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` will be generated, where `{pid}` will be the PID of the Node.js process, `{thread_id}` will be `0` when `writeHeapSnapshot()` is called from the main Node.js thread or the id of a worker thread." } ] } ], "desc": "

Generates a snapshot of the current V8 heap and writes it to a JSON\nfile. This file is intended to be used with tools such as Chrome\nDevTools. The JSON schema is undocumented and specific to the V8\nengine, and may change from one version of V8 to the next.

\n

A heap snapshot is specific to a single V8 isolate. When using\nworker threads, a heap snapshot generated from the main thread will\nnot contain any information about the workers, and vice versa.

\n
const { writeHeapSnapshot } = require('v8');\nconst {\n  Worker,\n  isMainThread,\n  parentPort\n} = require('worker_threads');\n\nif (isMainThread) {\n  const worker = new Worker(__filename);\n\n  worker.once('message', (filename) => {\n    console.log(`worker heapdump: ${filename}`);\n    // Now get a heapdump for the main thread.\n    console.log(`main thread heapdump: ${writeHeapSnapshot()}`);\n  });\n\n  // Tell the worker to create a heapdump.\n  worker.postMessage('heapdump');\n} else {\n  parentPort.once('message', (message) => {\n    if (message === 'heapdump') {\n      // Generate a heapdump for the worker\n      // and return the filename to the parent.\n      parentPort.postMessage(writeHeapSnapshot());\n    }\n  });\n}\n
" } ], "modules": [ { "textRaw": "Serialization API", "name": "serialization_api", "desc": "

The serialization API provides means of serializing JavaScript values in a way\nthat is compatible with the HTML structured clone algorithm.

\n

The format is backward-compatible (i.e. safe to store to disk).\nEqual JavaScript values may result in different serialized output.

", "methods": [ { "textRaw": "`v8.serialize(value)`", "type": "method", "name": "serialize", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`value` {any}", "name": "value", "type": "any" } ] } ], "desc": "

Uses a DefaultSerializer to serialize value into a buffer.

" }, { "textRaw": "`v8.deserialize(buffer)`", "type": "method", "name": "deserialize", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A buffer returned by [`serialize()`][].", "name": "buffer", "type": "Buffer|TypedArray|DataView", "desc": "A buffer returned by [`serialize()`][]." } ] } ], "desc": "

Uses a DefaultDeserializer with default options to read a JS value\nfrom a buffer.

" } ], "classes": [ { "textRaw": "Class: `v8.Serializer`", "type": "class", "name": "v8.Serializer", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "methods": [ { "textRaw": "`serializer.writeHeader()`", "type": "method", "name": "writeHeader", "signatures": [ { "params": [] } ], "desc": "

Writes out a header, which includes the serialization format version.

" }, { "textRaw": "`serializer.writeValue(value)`", "type": "method", "name": "writeValue", "signatures": [ { "params": [ { "textRaw": "`value` {any}", "name": "value", "type": "any" } ] } ], "desc": "

Serializes a JavaScript value and adds the serialized representation to the\ninternal buffer.

\n

This throws an error if value cannot be serialized.

" }, { "textRaw": "`serializer.releaseBuffer()`", "type": "method", "name": "releaseBuffer", "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [] } ], "desc": "

Returns the stored internal buffer. This serializer should not be used once\nthe buffer is released. Calling this method results in undefined behavior\nif a previous write has failed.

" }, { "textRaw": "`serializer.transferArrayBuffer(id, arrayBuffer)`", "type": "method", "name": "transferArrayBuffer", "signatures": [ { "params": [ { "textRaw": "`id` {integer} A 32-bit unsigned integer.", "name": "id", "type": "integer", "desc": "A 32-bit unsigned integer." }, { "textRaw": "`arrayBuffer` {ArrayBuffer} An `ArrayBuffer` instance.", "name": "arrayBuffer", "type": "ArrayBuffer", "desc": "An `ArrayBuffer` instance." } ] } ], "desc": "

Marks an ArrayBuffer as having its contents transferred out of band.\nPass the corresponding ArrayBuffer in the deserializing context to\ndeserializer.transferArrayBuffer().

" }, { "textRaw": "`serializer.writeUint32(value)`", "type": "method", "name": "writeUint32", "signatures": [ { "params": [ { "textRaw": "`value` {integer}", "name": "value", "type": "integer" } ] } ], "desc": "

Write a raw 32-bit unsigned integer.\nFor use inside of a custom serializer._writeHostObject().

" }, { "textRaw": "`serializer.writeUint64(hi, lo)`", "type": "method", "name": "writeUint64", "signatures": [ { "params": [ { "textRaw": "`hi` {integer}", "name": "hi", "type": "integer" }, { "textRaw": "`lo` {integer}", "name": "lo", "type": "integer" } ] } ], "desc": "

Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.\nFor use inside of a custom serializer._writeHostObject().

" }, { "textRaw": "`serializer.writeDouble(value)`", "type": "method", "name": "writeDouble", "signatures": [ { "params": [ { "textRaw": "`value` {number}", "name": "value", "type": "number" } ] } ], "desc": "

Write a JS number value.\nFor use inside of a custom serializer._writeHostObject().

" }, { "textRaw": "`serializer.writeRawBytes(buffer)`", "type": "method", "name": "writeRawBytes", "signatures": [ { "params": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView}", "name": "buffer", "type": "Buffer|TypedArray|DataView" } ] } ], "desc": "

Write raw bytes into the serializer’s internal buffer. The deserializer\nwill require a way to compute the length of the buffer.\nFor use inside of a custom serializer._writeHostObject().

" }, { "textRaw": "`serializer._writeHostObject(object)`", "type": "method", "name": "_writeHostObject", "signatures": [ { "params": [ { "textRaw": "`object` {Object}", "name": "object", "type": "Object" } ] } ], "desc": "

This method is called to write some kind of host object, i.e. an object created\nby native C++ bindings. If it is not possible to serialize object, a suitable\nexception should be thrown.

\n

This method is not present on the Serializer class itself but can be provided\nby subclasses.

" }, { "textRaw": "`serializer._getDataCloneError(message)`", "type": "method", "name": "_getDataCloneError", "signatures": [ { "params": [ { "textRaw": "`message` {string}", "name": "message", "type": "string" } ] } ], "desc": "

This method is called to generate error objects that will be thrown when an\nobject can not be cloned.

\n

This method defaults to the Error constructor and can be overridden on\nsubclasses.

" }, { "textRaw": "`serializer._getSharedArrayBufferId(sharedArrayBuffer)`", "type": "method", "name": "_getSharedArrayBufferId", "signatures": [ { "params": [ { "textRaw": "`sharedArrayBuffer` {SharedArrayBuffer}", "name": "sharedArrayBuffer", "type": "SharedArrayBuffer" } ] } ], "desc": "

This method is called when the serializer is going to serialize a\nSharedArrayBuffer object. It must return an unsigned 32-bit integer ID for\nthe object, using the same ID if this SharedArrayBuffer has already been\nserialized. When deserializing, this ID will be passed to\ndeserializer.transferArrayBuffer().

\n

If the object cannot be serialized, an exception should be thrown.

\n

This method is not present on the Serializer class itself but can be provided\nby subclasses.

" }, { "textRaw": "`serializer._setTreatArrayBufferViewsAsHostObjects(flag)`", "type": "method", "name": "_setTreatArrayBufferViewsAsHostObjects", "signatures": [ { "params": [ { "textRaw": "`flag` {boolean} **Default:** `false`", "name": "flag", "type": "boolean", "default": "`false`" } ] } ], "desc": "

Indicate whether to treat TypedArray and DataView objects as\nhost objects, i.e. pass them to serializer._writeHostObject().

" } ], "signatures": [ { "params": [], "desc": "

Creates a new Serializer object.

" } ] }, { "textRaw": "Class: `v8.Deserializer`", "type": "class", "name": "v8.Deserializer", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "methods": [ { "textRaw": "`deserializer.readHeader()`", "type": "method", "name": "readHeader", "signatures": [ { "params": [] } ], "desc": "

Reads and validates a header (including the format version).\nMay, for example, reject an invalid or unsupported wire format. In that case,\nan Error is thrown.

" }, { "textRaw": "`deserializer.readValue()`", "type": "method", "name": "readValue", "signatures": [ { "params": [] } ], "desc": "

Deserializes a JavaScript value from the buffer and returns it.

" }, { "textRaw": "`deserializer.transferArrayBuffer(id, arrayBuffer)`", "type": "method", "name": "transferArrayBuffer", "signatures": [ { "params": [ { "textRaw": "`id` {integer} A 32-bit unsigned integer.", "name": "id", "type": "integer", "desc": "A 32-bit unsigned integer." }, { "textRaw": "`arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An `ArrayBuffer` instance.", "name": "arrayBuffer", "type": "ArrayBuffer|SharedArrayBuffer", "desc": "An `ArrayBuffer` instance." } ] } ], "desc": "

Marks an ArrayBuffer as having its contents transferred out of band.\nPass the corresponding ArrayBuffer in the serializing context to\nserializer.transferArrayBuffer() (or return the id from\nserializer._getSharedArrayBufferId() in the case of SharedArrayBuffers).

" }, { "textRaw": "`deserializer.getWireFormatVersion()`", "type": "method", "name": "getWireFormatVersion", "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [] } ], "desc": "

Reads the underlying wire format version. Likely mostly to be useful to\nlegacy code reading old wire format versions. May not be called before\n.readHeader().

" }, { "textRaw": "`deserializer.readUint32()`", "type": "method", "name": "readUint32", "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [] } ], "desc": "

Read a raw 32-bit unsigned integer and return it.\nFor use inside of a custom deserializer._readHostObject().

" }, { "textRaw": "`deserializer.readUint64()`", "type": "method", "name": "readUint64", "signatures": [ { "return": { "textRaw": "Returns: {integer[]}", "name": "return", "type": "integer[]" }, "params": [] } ], "desc": "

Read a raw 64-bit unsigned integer and return it as an array [hi, lo]\nwith two 32-bit unsigned integer entries.\nFor use inside of a custom deserializer._readHostObject().

" }, { "textRaw": "`deserializer.readDouble()`", "type": "method", "name": "readDouble", "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [] } ], "desc": "

Read a JS number value.\nFor use inside of a custom deserializer._readHostObject().

" }, { "textRaw": "`deserializer.readRawBytes(length)`", "type": "method", "name": "readRawBytes", "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`length` {integer}", "name": "length", "type": "integer" } ] } ], "desc": "

Read raw bytes from the deserializer’s internal buffer. The length parameter\nmust correspond to the length of the buffer that was passed to\nserializer.writeRawBytes().\nFor use inside of a custom deserializer._readHostObject().

" }, { "textRaw": "`deserializer._readHostObject()`", "type": "method", "name": "_readHostObject", "signatures": [ { "params": [] } ], "desc": "

This method is called to read some kind of host object, i.e. an object that is\ncreated by native C++ bindings. If it is not possible to deserialize the data,\na suitable exception should be thrown.

\n

This method is not present on the Deserializer class itself but can be\nprovided by subclasses.

" } ], "signatures": [ { "params": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A buffer returned by [`serializer.releaseBuffer()`][].", "name": "buffer", "type": "Buffer|TypedArray|DataView", "desc": "A buffer returned by [`serializer.releaseBuffer()`][]." } ], "desc": "

Creates a new Deserializer object.

" } ] }, { "textRaw": "Class: `v8.DefaultSerializer`", "type": "class", "name": "v8.DefaultSerializer", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "desc": "

A subclass of Serializer that serializes TypedArray\n(in particular Buffer) and DataView objects as host objects, and only\nstores the part of their underlying ArrayBuffers that they are referring to.

" }, { "textRaw": "Class: `v8.DefaultDeserializer`", "type": "class", "name": "v8.DefaultDeserializer", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "desc": "

A subclass of Deserializer corresponding to the format written by\nDefaultSerializer.

" } ], "type": "module", "displayName": "Serialization API" } ], "type": "module", "displayName": "V8" } ] }