11cb0ef41Sopenharmony_ci# V8 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v4.0.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!-- source_link=lib/v8.js --> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciThe `node:v8` module exposes APIs that are specific to the version of [V8][] 81cb0ef41Sopenharmony_cibuilt into the Node.js binary. It can be accessed using: 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci```js 111cb0ef41Sopenharmony_ciconst v8 = require('node:v8'); 121cb0ef41Sopenharmony_ci``` 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci## `v8.cachedDataVersionTag()` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci<!-- YAML 171cb0ef41Sopenharmony_ciadded: v8.0.0 181cb0ef41Sopenharmony_ci--> 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci* Returns: {integer} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciReturns an integer representing a version tag derived from the V8 version, 231cb0ef41Sopenharmony_cicommand-line flags, and detected CPU features. This is useful for determining 241cb0ef41Sopenharmony_ciwhether a [`vm.Script`][] `cachedData` buffer is compatible with this instance 251cb0ef41Sopenharmony_ciof V8. 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci```js 281cb0ef41Sopenharmony_ciconsole.log(v8.cachedDataVersionTag()); // 3947234607 291cb0ef41Sopenharmony_ci// The value returned by v8.cachedDataVersionTag() is derived from the V8 301cb0ef41Sopenharmony_ci// version, command-line flags, and detected CPU features. Test that the value 311cb0ef41Sopenharmony_ci// does indeed update when flags are toggled. 321cb0ef41Sopenharmony_civ8.setFlagsFromString('--allow_natives_syntax'); 331cb0ef41Sopenharmony_ciconsole.log(v8.cachedDataVersionTag()); // 183726201 341cb0ef41Sopenharmony_ci``` 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci## `v8.getHeapCodeStatistics()` 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci<!-- YAML 391cb0ef41Sopenharmony_ciadded: v12.8.0 401cb0ef41Sopenharmony_ci--> 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci* Returns: {Object} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciGet statistics about code and its metadata in the heap, see V8 451cb0ef41Sopenharmony_ci[`GetHeapCodeAndMetadataStatistics`][] API. Returns an object with the 461cb0ef41Sopenharmony_cifollowing properties: 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci* `code_and_metadata_size` {number} 491cb0ef41Sopenharmony_ci* `bytecode_and_metadata_size` {number} 501cb0ef41Sopenharmony_ci* `external_script_source_size` {number} 511cb0ef41Sopenharmony_ci* `cpu_profiler_metadata_size` {number} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci<!-- eslint-skip --> 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci```js 561cb0ef41Sopenharmony_ci{ 571cb0ef41Sopenharmony_ci code_and_metadata_size: 212208, 581cb0ef41Sopenharmony_ci bytecode_and_metadata_size: 161368, 591cb0ef41Sopenharmony_ci external_script_source_size: 1410794, 601cb0ef41Sopenharmony_ci cpu_profiler_metadata_size: 0, 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci``` 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci## `v8.getHeapSnapshot()` 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci<!-- YAML 671cb0ef41Sopenharmony_ciadded: v11.13.0 681cb0ef41Sopenharmony_ci--> 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci* Returns: {stream.Readable} A Readable Stream containing the V8 heap snapshot 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciGenerates a snapshot of the current V8 heap and returns a Readable 731cb0ef41Sopenharmony_ciStream that may be used to read the JSON serialized representation. 741cb0ef41Sopenharmony_ciThis JSON stream format is intended to be used with tools such as 751cb0ef41Sopenharmony_ciChrome DevTools. The JSON schema is undocumented and specific to the 761cb0ef41Sopenharmony_ciV8 engine. Therefore, the schema may change from one version of V8 to the next. 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciCreating a heap snapshot requires memory about twice the size of the heap at 791cb0ef41Sopenharmony_cithe time the snapshot is created. This results in the risk of OOM killers 801cb0ef41Sopenharmony_citerminating the process. 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciGenerating a snapshot is a synchronous operation which blocks the event loop 831cb0ef41Sopenharmony_cifor a duration depending on the heap size. 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci```js 861cb0ef41Sopenharmony_ci// Print heap snapshot to the console 871cb0ef41Sopenharmony_ciconst v8 = require('node:v8'); 881cb0ef41Sopenharmony_ciconst stream = v8.getHeapSnapshot(); 891cb0ef41Sopenharmony_cistream.pipe(process.stdout); 901cb0ef41Sopenharmony_ci``` 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci## `v8.getHeapSpaceStatistics()` 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci<!-- YAML 951cb0ef41Sopenharmony_ciadded: v6.0.0 961cb0ef41Sopenharmony_cichanges: 971cb0ef41Sopenharmony_ci - version: v7.5.0 981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/10186 991cb0ef41Sopenharmony_ci description: Support values exceeding the 32-bit unsigned integer range. 1001cb0ef41Sopenharmony_ci--> 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci* Returns: {Object\[]} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ciReturns statistics about the V8 heap spaces, i.e. the segments which make up 1051cb0ef41Sopenharmony_cithe V8 heap. Neither the ordering of heap spaces, nor the availability of a 1061cb0ef41Sopenharmony_ciheap space can be guaranteed as the statistics are provided via the V8 1071cb0ef41Sopenharmony_ci[`GetHeapSpaceStatistics`][] function and may change from one V8 version to the 1081cb0ef41Sopenharmony_cinext. 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciThe value returned is an array of objects containing the following properties: 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci* `space_name` {string} 1131cb0ef41Sopenharmony_ci* `space_size` {number} 1141cb0ef41Sopenharmony_ci* `space_used_size` {number} 1151cb0ef41Sopenharmony_ci* `space_available_size` {number} 1161cb0ef41Sopenharmony_ci* `physical_space_size` {number} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci```json 1191cb0ef41Sopenharmony_ci[ 1201cb0ef41Sopenharmony_ci { 1211cb0ef41Sopenharmony_ci "space_name": "new_space", 1221cb0ef41Sopenharmony_ci "space_size": 2063872, 1231cb0ef41Sopenharmony_ci "space_used_size": 951112, 1241cb0ef41Sopenharmony_ci "space_available_size": 80824, 1251cb0ef41Sopenharmony_ci "physical_space_size": 2063872 1261cb0ef41Sopenharmony_ci }, 1271cb0ef41Sopenharmony_ci { 1281cb0ef41Sopenharmony_ci "space_name": "old_space", 1291cb0ef41Sopenharmony_ci "space_size": 3090560, 1301cb0ef41Sopenharmony_ci "space_used_size": 2493792, 1311cb0ef41Sopenharmony_ci "space_available_size": 0, 1321cb0ef41Sopenharmony_ci "physical_space_size": 3090560 1331cb0ef41Sopenharmony_ci }, 1341cb0ef41Sopenharmony_ci { 1351cb0ef41Sopenharmony_ci "space_name": "code_space", 1361cb0ef41Sopenharmony_ci "space_size": 1260160, 1371cb0ef41Sopenharmony_ci "space_used_size": 644256, 1381cb0ef41Sopenharmony_ci "space_available_size": 960, 1391cb0ef41Sopenharmony_ci "physical_space_size": 1260160 1401cb0ef41Sopenharmony_ci }, 1411cb0ef41Sopenharmony_ci { 1421cb0ef41Sopenharmony_ci "space_name": "map_space", 1431cb0ef41Sopenharmony_ci "space_size": 1094160, 1441cb0ef41Sopenharmony_ci "space_used_size": 201608, 1451cb0ef41Sopenharmony_ci "space_available_size": 0, 1461cb0ef41Sopenharmony_ci "physical_space_size": 1094160 1471cb0ef41Sopenharmony_ci }, 1481cb0ef41Sopenharmony_ci { 1491cb0ef41Sopenharmony_ci "space_name": "large_object_space", 1501cb0ef41Sopenharmony_ci "space_size": 0, 1511cb0ef41Sopenharmony_ci "space_used_size": 0, 1521cb0ef41Sopenharmony_ci "space_available_size": 1490980608, 1531cb0ef41Sopenharmony_ci "physical_space_size": 0 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci] 1561cb0ef41Sopenharmony_ci``` 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci## `v8.getHeapStatistics()` 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci<!-- YAML 1611cb0ef41Sopenharmony_ciadded: v1.0.0 1621cb0ef41Sopenharmony_cichanges: 1631cb0ef41Sopenharmony_ci - version: v7.5.0 1641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/10186 1651cb0ef41Sopenharmony_ci description: Support values exceeding the 32-bit unsigned integer range. 1661cb0ef41Sopenharmony_ci - version: v7.2.0 1671cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/8610 1681cb0ef41Sopenharmony_ci description: Added `malloced_memory`, `peak_malloced_memory`, 1691cb0ef41Sopenharmony_ci and `does_zap_garbage`. 1701cb0ef41Sopenharmony_ci--> 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci* Returns: {Object} 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ciReturns an object with the following properties: 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci* `total_heap_size` {number} 1771cb0ef41Sopenharmony_ci* `total_heap_size_executable` {number} 1781cb0ef41Sopenharmony_ci* `total_physical_size` {number} 1791cb0ef41Sopenharmony_ci* `total_available_size` {number} 1801cb0ef41Sopenharmony_ci* `used_heap_size` {number} 1811cb0ef41Sopenharmony_ci* `heap_size_limit` {number} 1821cb0ef41Sopenharmony_ci* `malloced_memory` {number} 1831cb0ef41Sopenharmony_ci* `peak_malloced_memory` {number} 1841cb0ef41Sopenharmony_ci* `does_zap_garbage` {number} 1851cb0ef41Sopenharmony_ci* `number_of_native_contexts` {number} 1861cb0ef41Sopenharmony_ci* `number_of_detached_contexts` {number} 1871cb0ef41Sopenharmony_ci* `total_global_handles_size` {number} 1881cb0ef41Sopenharmony_ci* `used_global_handles_size` {number} 1891cb0ef41Sopenharmony_ci* `external_memory` {number} 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci`does_zap_garbage` is a 0/1 boolean, which signifies whether the 1921cb0ef41Sopenharmony_ci`--zap_code_space` option is enabled or not. This makes V8 overwrite heap 1931cb0ef41Sopenharmony_cigarbage with a bit pattern. The RSS footprint (resident set size) gets bigger 1941cb0ef41Sopenharmony_cibecause it continuously touches all heap pages and that makes them less likely 1951cb0ef41Sopenharmony_cito get swapped out by the operating system. 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci`number_of_native_contexts` The value of native\_context is the number of the 1981cb0ef41Sopenharmony_citop-level contexts currently active. Increase of this number over time indicates 1991cb0ef41Sopenharmony_cia memory leak. 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci`number_of_detached_contexts` The value of detached\_context is the number 2021cb0ef41Sopenharmony_ciof contexts that were detached and not yet garbage collected. This number 2031cb0ef41Sopenharmony_cibeing non-zero indicates a potential memory leak. 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci`total_global_handles_size` The value of total\_global\_handles\_size is the 2061cb0ef41Sopenharmony_citotal memory size of V8 global handles. 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci`used_global_handles_size` The value of used\_global\_handles\_size is the 2091cb0ef41Sopenharmony_ciused memory size of V8 global handles. 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci`external_memory` The value of external\_memory is the memory size of array 2121cb0ef41Sopenharmony_cibuffers and external strings. 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci<!-- eslint-skip --> 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci```js 2171cb0ef41Sopenharmony_ci{ 2181cb0ef41Sopenharmony_ci total_heap_size: 7326976, 2191cb0ef41Sopenharmony_ci total_heap_size_executable: 4194304, 2201cb0ef41Sopenharmony_ci total_physical_size: 7326976, 2211cb0ef41Sopenharmony_ci total_available_size: 1152656, 2221cb0ef41Sopenharmony_ci used_heap_size: 3476208, 2231cb0ef41Sopenharmony_ci heap_size_limit: 1535115264, 2241cb0ef41Sopenharmony_ci malloced_memory: 16384, 2251cb0ef41Sopenharmony_ci peak_malloced_memory: 1127496, 2261cb0ef41Sopenharmony_ci does_zap_garbage: 0, 2271cb0ef41Sopenharmony_ci number_of_native_contexts: 1, 2281cb0ef41Sopenharmony_ci number_of_detached_contexts: 0, 2291cb0ef41Sopenharmony_ci total_global_handles_size: 8192, 2301cb0ef41Sopenharmony_ci used_global_handles_size: 3296, 2311cb0ef41Sopenharmony_ci external_memory: 318824 2321cb0ef41Sopenharmony_ci} 2331cb0ef41Sopenharmony_ci``` 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci## `v8.setFlagsFromString(flags)` 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci<!-- YAML 2381cb0ef41Sopenharmony_ciadded: v1.0.0 2391cb0ef41Sopenharmony_ci--> 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci* `flags` {string} 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ciThe `v8.setFlagsFromString()` method can be used to programmatically set 2441cb0ef41Sopenharmony_ciV8 command-line flags. This method should be used with care. Changing settings 2451cb0ef41Sopenharmony_ciafter the VM has started may result in unpredictable behavior, including 2461cb0ef41Sopenharmony_cicrashes and data loss; or it may simply do nothing. 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciThe V8 options available for a version of Node.js may be determined by running 2491cb0ef41Sopenharmony_ci`node --v8-options`. 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ciUsage: 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci```js 2541cb0ef41Sopenharmony_ci// Print GC events to stdout for one minute. 2551cb0ef41Sopenharmony_ciconst v8 = require('node:v8'); 2561cb0ef41Sopenharmony_civ8.setFlagsFromString('--trace_gc'); 2571cb0ef41Sopenharmony_cisetTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3); 2581cb0ef41Sopenharmony_ci``` 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci## `v8.stopCoverage()` 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci<!-- YAML 2631cb0ef41Sopenharmony_ciadded: 2641cb0ef41Sopenharmony_ci - v15.1.0 2651cb0ef41Sopenharmony_ci - v14.18.0 2661cb0ef41Sopenharmony_ci - v12.22.0 2671cb0ef41Sopenharmony_ci--> 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ciThe `v8.stopCoverage()` method allows the user to stop the coverage collection 2701cb0ef41Sopenharmony_cistarted by [`NODE_V8_COVERAGE`][], so that V8 can release the execution count 2711cb0ef41Sopenharmony_cirecords and optimize code. This can be used in conjunction with 2721cb0ef41Sopenharmony_ci[`v8.takeCoverage()`][] if the user wants to collect the coverage on demand. 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci## `v8.takeCoverage()` 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci<!-- YAML 2771cb0ef41Sopenharmony_ciadded: 2781cb0ef41Sopenharmony_ci - v15.1.0 2791cb0ef41Sopenharmony_ci - v14.18.0 2801cb0ef41Sopenharmony_ci - v12.22.0 2811cb0ef41Sopenharmony_ci--> 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ciThe `v8.takeCoverage()` method allows the user to write the coverage started by 2841cb0ef41Sopenharmony_ci[`NODE_V8_COVERAGE`][] to disk on demand. This method can be invoked multiple 2851cb0ef41Sopenharmony_citimes during the lifetime of the process. Each time the execution counter will 2861cb0ef41Sopenharmony_cibe reset and a new coverage report will be written to the directory specified 2871cb0ef41Sopenharmony_ciby [`NODE_V8_COVERAGE`][]. 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ciWhen the process is about to exit, one last coverage will still be written to 2901cb0ef41Sopenharmony_cidisk unless [`v8.stopCoverage()`][] is invoked before the process exits. 2911cb0ef41Sopenharmony_ci 2921cb0ef41Sopenharmony_ci## `v8.writeHeapSnapshot([filename])` 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci<!-- YAML 2951cb0ef41Sopenharmony_ciadded: v11.13.0 2961cb0ef41Sopenharmony_cichanges: 2971cb0ef41Sopenharmony_ci - version: v18.0.0 2981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41373 2991cb0ef41Sopenharmony_ci description: An exception will now be thrown if the file could not be written. 3001cb0ef41Sopenharmony_ci - version: v18.0.0 3011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42577 3021cb0ef41Sopenharmony_ci description: Make the returned error codes consistent across all platforms. 3031cb0ef41Sopenharmony_ci--> 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci* `filename` {string} The file path where the V8 heap snapshot is to be 3061cb0ef41Sopenharmony_ci saved. If not specified, a file name with the pattern 3071cb0ef41Sopenharmony_ci `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` will be 3081cb0ef41Sopenharmony_ci generated, where `{pid}` will be the PID of the Node.js process, 3091cb0ef41Sopenharmony_ci `{thread_id}` will be `0` when `writeHeapSnapshot()` is called from 3101cb0ef41Sopenharmony_ci the main Node.js thread or the id of a worker thread. 3111cb0ef41Sopenharmony_ci* Returns: {string} The filename where the snapshot was saved. 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ciGenerates a snapshot of the current V8 heap and writes it to a JSON 3141cb0ef41Sopenharmony_cifile. This file is intended to be used with tools such as Chrome 3151cb0ef41Sopenharmony_ciDevTools. The JSON schema is undocumented and specific to the V8 3161cb0ef41Sopenharmony_ciengine, and may change from one version of V8 to the next. 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ciA heap snapshot is specific to a single V8 isolate. When using 3191cb0ef41Sopenharmony_ci[worker threads][], a heap snapshot generated from the main thread will 3201cb0ef41Sopenharmony_cinot contain any information about the workers, and vice versa. 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ciCreating a heap snapshot requires memory about twice the size of the heap at 3231cb0ef41Sopenharmony_cithe time the snapshot is created. This results in the risk of OOM killers 3241cb0ef41Sopenharmony_citerminating the process. 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ciGenerating a snapshot is a synchronous operation which blocks the event loop 3271cb0ef41Sopenharmony_cifor a duration depending on the heap size. 3281cb0ef41Sopenharmony_ci 3291cb0ef41Sopenharmony_ci```js 3301cb0ef41Sopenharmony_ciconst { writeHeapSnapshot } = require('node:v8'); 3311cb0ef41Sopenharmony_ciconst { 3321cb0ef41Sopenharmony_ci Worker, 3331cb0ef41Sopenharmony_ci isMainThread, 3341cb0ef41Sopenharmony_ci parentPort, 3351cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ciif (isMainThread) { 3381cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci worker.once('message', (filename) => { 3411cb0ef41Sopenharmony_ci console.log(`worker heapdump: ${filename}`); 3421cb0ef41Sopenharmony_ci // Now get a heapdump for the main thread. 3431cb0ef41Sopenharmony_ci console.log(`main thread heapdump: ${writeHeapSnapshot()}`); 3441cb0ef41Sopenharmony_ci }); 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci // Tell the worker to create a heapdump. 3471cb0ef41Sopenharmony_ci worker.postMessage('heapdump'); 3481cb0ef41Sopenharmony_ci} else { 3491cb0ef41Sopenharmony_ci parentPort.once('message', (message) => { 3501cb0ef41Sopenharmony_ci if (message === 'heapdump') { 3511cb0ef41Sopenharmony_ci // Generate a heapdump for the worker 3521cb0ef41Sopenharmony_ci // and return the filename to the parent. 3531cb0ef41Sopenharmony_ci parentPort.postMessage(writeHeapSnapshot()); 3541cb0ef41Sopenharmony_ci } 3551cb0ef41Sopenharmony_ci }); 3561cb0ef41Sopenharmony_ci} 3571cb0ef41Sopenharmony_ci``` 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci## `v8.setHeapSnapshotNearHeapLimit(limit)` 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci<!-- YAML 3621cb0ef41Sopenharmony_ciadded: v18.10.0 3631cb0ef41Sopenharmony_ci--> 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 3661cb0ef41Sopenharmony_ci 3671cb0ef41Sopenharmony_ci* `limit` {integer} 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ciThe API is a no-op if `--heapsnapshot-near-heap-limit` is already set from the 3701cb0ef41Sopenharmony_cicommand line or the API is called more than once. `limit` must be a positive 3711cb0ef41Sopenharmony_ciinteger. See [`--heapsnapshot-near-heap-limit`][] for more information. 3721cb0ef41Sopenharmony_ci 3731cb0ef41Sopenharmony_ci## Serialization API 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ciThe serialization API provides means of serializing JavaScript values in a way 3761cb0ef41Sopenharmony_cithat is compatible with the [HTML structured clone algorithm][]. 3771cb0ef41Sopenharmony_ci 3781cb0ef41Sopenharmony_ciThe format is backward-compatible (i.e. safe to store to disk). 3791cb0ef41Sopenharmony_ciEqual JavaScript values may result in different serialized output. 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_ci### `v8.serialize(value)` 3821cb0ef41Sopenharmony_ci 3831cb0ef41Sopenharmony_ci<!-- YAML 3841cb0ef41Sopenharmony_ciadded: v8.0.0 3851cb0ef41Sopenharmony_ci--> 3861cb0ef41Sopenharmony_ci 3871cb0ef41Sopenharmony_ci* `value` {any} 3881cb0ef41Sopenharmony_ci* Returns: {Buffer} 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_ciUses a [`DefaultSerializer`][] to serialize `value` into a buffer. 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_ci[`ERR_BUFFER_TOO_LARGE`][] will be thrown when trying to 3931cb0ef41Sopenharmony_ciserialize a huge object which requires buffer 3941cb0ef41Sopenharmony_cilarger than [`buffer.constants.MAX_LENGTH`][]. 3951cb0ef41Sopenharmony_ci 3961cb0ef41Sopenharmony_ci### `v8.deserialize(buffer)` 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ci<!-- YAML 3991cb0ef41Sopenharmony_ciadded: v8.0.0 4001cb0ef41Sopenharmony_ci--> 4011cb0ef41Sopenharmony_ci 4021cb0ef41Sopenharmony_ci* `buffer` {Buffer|TypedArray|DataView} A buffer returned by [`serialize()`][]. 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ciUses a [`DefaultDeserializer`][] with default options to read a JS value 4051cb0ef41Sopenharmony_cifrom a buffer. 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci### Class: `v8.Serializer` 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci<!-- YAML 4101cb0ef41Sopenharmony_ciadded: v8.0.0 4111cb0ef41Sopenharmony_ci--> 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci#### `new Serializer()` 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ciCreates a new `Serializer` object. 4161cb0ef41Sopenharmony_ci 4171cb0ef41Sopenharmony_ci#### `serializer.writeHeader()` 4181cb0ef41Sopenharmony_ci 4191cb0ef41Sopenharmony_ciWrites out a header, which includes the serialization format version. 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_ci#### `serializer.writeValue(value)` 4221cb0ef41Sopenharmony_ci 4231cb0ef41Sopenharmony_ci* `value` {any} 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ciSerializes a JavaScript value and adds the serialized representation to the 4261cb0ef41Sopenharmony_ciinternal buffer. 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ciThis throws an error if `value` cannot be serialized. 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci#### `serializer.releaseBuffer()` 4311cb0ef41Sopenharmony_ci 4321cb0ef41Sopenharmony_ci* Returns: {Buffer} 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ciReturns the stored internal buffer. This serializer should not be used once 4351cb0ef41Sopenharmony_cithe buffer is released. Calling this method results in undefined behavior 4361cb0ef41Sopenharmony_ciif a previous write has failed. 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ci#### `serializer.transferArrayBuffer(id, arrayBuffer)` 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_ci* `id` {integer} A 32-bit unsigned integer. 4411cb0ef41Sopenharmony_ci* `arrayBuffer` {ArrayBuffer} An `ArrayBuffer` instance. 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ciMarks an `ArrayBuffer` as having its contents transferred out of band. 4441cb0ef41Sopenharmony_ciPass the corresponding `ArrayBuffer` in the deserializing context to 4451cb0ef41Sopenharmony_ci[`deserializer.transferArrayBuffer()`][]. 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci#### `serializer.writeUint32(value)` 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ci* `value` {integer} 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ciWrite a raw 32-bit unsigned integer. 4521cb0ef41Sopenharmony_ciFor use inside of a custom [`serializer._writeHostObject()`][]. 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci#### `serializer.writeUint64(hi, lo)` 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci* `hi` {integer} 4571cb0ef41Sopenharmony_ci* `lo` {integer} 4581cb0ef41Sopenharmony_ci 4591cb0ef41Sopenharmony_ciWrite a raw 64-bit unsigned integer, split into high and low 32-bit parts. 4601cb0ef41Sopenharmony_ciFor use inside of a custom [`serializer._writeHostObject()`][]. 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ci#### `serializer.writeDouble(value)` 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci* `value` {number} 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ciWrite a JS `number` value. 4671cb0ef41Sopenharmony_ciFor use inside of a custom [`serializer._writeHostObject()`][]. 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_ci#### `serializer.writeRawBytes(buffer)` 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ci* `buffer` {Buffer|TypedArray|DataView} 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ciWrite raw bytes into the serializer's internal buffer. The deserializer 4741cb0ef41Sopenharmony_ciwill require a way to compute the length of the buffer. 4751cb0ef41Sopenharmony_ciFor use inside of a custom [`serializer._writeHostObject()`][]. 4761cb0ef41Sopenharmony_ci 4771cb0ef41Sopenharmony_ci#### `serializer._writeHostObject(object)` 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci* `object` {Object} 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ciThis method is called to write some kind of host object, i.e. an object created 4821cb0ef41Sopenharmony_ciby native C++ bindings. If it is not possible to serialize `object`, a suitable 4831cb0ef41Sopenharmony_ciexception should be thrown. 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ciThis method is not present on the `Serializer` class itself but can be provided 4861cb0ef41Sopenharmony_ciby subclasses. 4871cb0ef41Sopenharmony_ci 4881cb0ef41Sopenharmony_ci#### `serializer._getDataCloneError(message)` 4891cb0ef41Sopenharmony_ci 4901cb0ef41Sopenharmony_ci* `message` {string} 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ciThis method is called to generate error objects that will be thrown when an 4931cb0ef41Sopenharmony_ciobject can not be cloned. 4941cb0ef41Sopenharmony_ci 4951cb0ef41Sopenharmony_ciThis method defaults to the [`Error`][] constructor and can be overridden on 4961cb0ef41Sopenharmony_cisubclasses. 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci#### `serializer._getSharedArrayBufferId(sharedArrayBuffer)` 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci* `sharedArrayBuffer` {SharedArrayBuffer} 5011cb0ef41Sopenharmony_ci 5021cb0ef41Sopenharmony_ciThis method is called when the serializer is going to serialize a 5031cb0ef41Sopenharmony_ci`SharedArrayBuffer` object. It must return an unsigned 32-bit integer ID for 5041cb0ef41Sopenharmony_cithe object, using the same ID if this `SharedArrayBuffer` has already been 5051cb0ef41Sopenharmony_ciserialized. When deserializing, this ID will be passed to 5061cb0ef41Sopenharmony_ci[`deserializer.transferArrayBuffer()`][]. 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciIf the object cannot be serialized, an exception should be thrown. 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ciThis method is not present on the `Serializer` class itself but can be provided 5111cb0ef41Sopenharmony_ciby subclasses. 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci#### `serializer._setTreatArrayBufferViewsAsHostObjects(flag)` 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci* `flag` {boolean} **Default:** `false` 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ciIndicate whether to treat `TypedArray` and `DataView` objects as 5181cb0ef41Sopenharmony_cihost objects, i.e. pass them to [`serializer._writeHostObject()`][]. 5191cb0ef41Sopenharmony_ci 5201cb0ef41Sopenharmony_ci### Class: `v8.Deserializer` 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci<!-- YAML 5231cb0ef41Sopenharmony_ciadded: v8.0.0 5241cb0ef41Sopenharmony_ci--> 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_ci#### `new Deserializer(buffer)` 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci* `buffer` {Buffer|TypedArray|DataView} A buffer returned by 5291cb0ef41Sopenharmony_ci [`serializer.releaseBuffer()`][]. 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ciCreates a new `Deserializer` object. 5321cb0ef41Sopenharmony_ci 5331cb0ef41Sopenharmony_ci#### `deserializer.readHeader()` 5341cb0ef41Sopenharmony_ci 5351cb0ef41Sopenharmony_ciReads and validates a header (including the format version). 5361cb0ef41Sopenharmony_ciMay, for example, reject an invalid or unsupported wire format. In that case, 5371cb0ef41Sopenharmony_cian `Error` is thrown. 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ci#### `deserializer.readValue()` 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ciDeserializes a JavaScript value from the buffer and returns it. 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ci#### `deserializer.transferArrayBuffer(id, arrayBuffer)` 5441cb0ef41Sopenharmony_ci 5451cb0ef41Sopenharmony_ci* `id` {integer} A 32-bit unsigned integer. 5461cb0ef41Sopenharmony_ci* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An `ArrayBuffer` instance. 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ciMarks an `ArrayBuffer` as having its contents transferred out of band. 5491cb0ef41Sopenharmony_ciPass the corresponding `ArrayBuffer` in the serializing context to 5501cb0ef41Sopenharmony_ci[`serializer.transferArrayBuffer()`][] (or return the `id` from 5511cb0ef41Sopenharmony_ci[`serializer._getSharedArrayBufferId()`][] in the case of `SharedArrayBuffer`s). 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ci#### `deserializer.getWireFormatVersion()` 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ci* Returns: {integer} 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ciReads the underlying wire format version. Likely mostly to be useful to 5581cb0ef41Sopenharmony_cilegacy code reading old wire format versions. May not be called before 5591cb0ef41Sopenharmony_ci`.readHeader()`. 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci#### `deserializer.readUint32()` 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci* Returns: {integer} 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ciRead a raw 32-bit unsigned integer and return it. 5661cb0ef41Sopenharmony_ciFor use inside of a custom [`deserializer._readHostObject()`][]. 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ci#### `deserializer.readUint64()` 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ci* Returns: {integer\[]} 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ciRead a raw 64-bit unsigned integer and return it as an array `[hi, lo]` 5731cb0ef41Sopenharmony_ciwith two 32-bit unsigned integer entries. 5741cb0ef41Sopenharmony_ciFor use inside of a custom [`deserializer._readHostObject()`][]. 5751cb0ef41Sopenharmony_ci 5761cb0ef41Sopenharmony_ci#### `deserializer.readDouble()` 5771cb0ef41Sopenharmony_ci 5781cb0ef41Sopenharmony_ci* Returns: {number} 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ciRead a JS `number` value. 5811cb0ef41Sopenharmony_ciFor use inside of a custom [`deserializer._readHostObject()`][]. 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ci#### `deserializer.readRawBytes(length)` 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci* `length` {integer} 5861cb0ef41Sopenharmony_ci* Returns: {Buffer} 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ciRead raw bytes from the deserializer's internal buffer. The `length` parameter 5891cb0ef41Sopenharmony_cimust correspond to the length of the buffer that was passed to 5901cb0ef41Sopenharmony_ci[`serializer.writeRawBytes()`][]. 5911cb0ef41Sopenharmony_ciFor use inside of a custom [`deserializer._readHostObject()`][]. 5921cb0ef41Sopenharmony_ci 5931cb0ef41Sopenharmony_ci#### `deserializer._readHostObject()` 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ciThis method is called to read some kind of host object, i.e. an object that is 5961cb0ef41Sopenharmony_cicreated by native C++ bindings. If it is not possible to deserialize the data, 5971cb0ef41Sopenharmony_cia suitable exception should be thrown. 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ciThis method is not present on the `Deserializer` class itself but can be 6001cb0ef41Sopenharmony_ciprovided by subclasses. 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ci### Class: `v8.DefaultSerializer` 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci<!-- YAML 6051cb0ef41Sopenharmony_ciadded: v8.0.0 6061cb0ef41Sopenharmony_ci--> 6071cb0ef41Sopenharmony_ci 6081cb0ef41Sopenharmony_ciA subclass of [`Serializer`][] that serializes `TypedArray` 6091cb0ef41Sopenharmony_ci(in particular [`Buffer`][]) and `DataView` objects as host objects, and only 6101cb0ef41Sopenharmony_cistores the part of their underlying `ArrayBuffer`s that they are referring to. 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci### Class: `v8.DefaultDeserializer` 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_ci<!-- YAML 6151cb0ef41Sopenharmony_ciadded: v8.0.0 6161cb0ef41Sopenharmony_ci--> 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ciA subclass of [`Deserializer`][] corresponding to the format written by 6191cb0ef41Sopenharmony_ci[`DefaultSerializer`][]. 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci## Promise hooks 6221cb0ef41Sopenharmony_ci 6231cb0ef41Sopenharmony_ciThe `promiseHooks` interface can be used to track promise lifecycle events. 6241cb0ef41Sopenharmony_ciTo track _all_ async activity, see [`async_hooks`][] which internally uses this 6251cb0ef41Sopenharmony_cimodule to produce promise lifecycle events in addition to events for other 6261cb0ef41Sopenharmony_ciasync resources. For request context management, see [`AsyncLocalStorage`][]. 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_ci```mjs 6291cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 6301cb0ef41Sopenharmony_ci 6311cb0ef41Sopenharmony_ci// There are four lifecycle events produced by promises: 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci// The `init` event represents the creation of a promise. This could be a 6341cb0ef41Sopenharmony_ci// direct creation such as with `new Promise(...)` or a continuation such 6351cb0ef41Sopenharmony_ci// as `then()` or `catch()`. It also happens whenever an async function is 6361cb0ef41Sopenharmony_ci// called or does an `await`. If a continuation promise is created, the 6371cb0ef41Sopenharmony_ci// `parent` will be the promise it is a continuation from. 6381cb0ef41Sopenharmony_cifunction init(promise, parent) { 6391cb0ef41Sopenharmony_ci console.log('a promise was created', { promise, parent }); 6401cb0ef41Sopenharmony_ci} 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci// The `settled` event happens when a promise receives a resolution or 6431cb0ef41Sopenharmony_ci// rejection value. This may happen synchronously such as when using 6441cb0ef41Sopenharmony_ci// `Promise.resolve()` on non-promise input. 6451cb0ef41Sopenharmony_cifunction settled(promise) { 6461cb0ef41Sopenharmony_ci console.log('a promise resolved or rejected', { promise }); 6471cb0ef41Sopenharmony_ci} 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ci// The `before` event runs immediately before a `then()` or `catch()` handler 6501cb0ef41Sopenharmony_ci// runs or an `await` resumes execution. 6511cb0ef41Sopenharmony_cifunction before(promise) { 6521cb0ef41Sopenharmony_ci console.log('a promise is about to call a then handler', { promise }); 6531cb0ef41Sopenharmony_ci} 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci// The `after` event runs immediately after a `then()` handler runs or when 6561cb0ef41Sopenharmony_ci// an `await` begins after resuming from another. 6571cb0ef41Sopenharmony_cifunction after(promise) { 6581cb0ef41Sopenharmony_ci console.log('a promise is done calling a then handler', { promise }); 6591cb0ef41Sopenharmony_ci} 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci// Lifecycle hooks may be started and stopped individually 6621cb0ef41Sopenharmony_ciconst stopWatchingInits = promiseHooks.onInit(init); 6631cb0ef41Sopenharmony_ciconst stopWatchingSettleds = promiseHooks.onSettled(settled); 6641cb0ef41Sopenharmony_ciconst stopWatchingBefores = promiseHooks.onBefore(before); 6651cb0ef41Sopenharmony_ciconst stopWatchingAfters = promiseHooks.onAfter(after); 6661cb0ef41Sopenharmony_ci 6671cb0ef41Sopenharmony_ci// Or they may be started and stopped in groups 6681cb0ef41Sopenharmony_ciconst stopHookSet = promiseHooks.createHook({ 6691cb0ef41Sopenharmony_ci init, 6701cb0ef41Sopenharmony_ci settled, 6711cb0ef41Sopenharmony_ci before, 6721cb0ef41Sopenharmony_ci after, 6731cb0ef41Sopenharmony_ci}); 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci// To stop a hook, call the function returned at its creation. 6761cb0ef41Sopenharmony_cistopWatchingInits(); 6771cb0ef41Sopenharmony_cistopWatchingSettleds(); 6781cb0ef41Sopenharmony_cistopWatchingBefores(); 6791cb0ef41Sopenharmony_cistopWatchingAfters(); 6801cb0ef41Sopenharmony_cistopHookSet(); 6811cb0ef41Sopenharmony_ci``` 6821cb0ef41Sopenharmony_ci 6831cb0ef41Sopenharmony_ci### `promiseHooks.onInit(init)` 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ci<!-- YAML 6861cb0ef41Sopenharmony_ciadded: 6871cb0ef41Sopenharmony_ci - v17.1.0 6881cb0ef41Sopenharmony_ci - v16.14.0 6891cb0ef41Sopenharmony_ci--> 6901cb0ef41Sopenharmony_ci 6911cb0ef41Sopenharmony_ci* `init` {Function} The [`init` callback][] to call when a promise is created. 6921cb0ef41Sopenharmony_ci* Returns: {Function} Call to stop the hook. 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ci**The `init` hook must be a plain function. Providing an async function will 6951cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.** 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ci```mjs 6981cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ciconst stop = promiseHooks.onInit((promise, parent) => {}); 7011cb0ef41Sopenharmony_ci``` 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ci```cjs 7041cb0ef41Sopenharmony_ciconst { promiseHooks } = require('node:v8'); 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ciconst stop = promiseHooks.onInit((promise, parent) => {}); 7071cb0ef41Sopenharmony_ci``` 7081cb0ef41Sopenharmony_ci 7091cb0ef41Sopenharmony_ci### `promiseHooks.onSettled(settled)` 7101cb0ef41Sopenharmony_ci 7111cb0ef41Sopenharmony_ci<!-- YAML 7121cb0ef41Sopenharmony_ciadded: 7131cb0ef41Sopenharmony_ci - v17.1.0 7141cb0ef41Sopenharmony_ci - v16.14.0 7151cb0ef41Sopenharmony_ci--> 7161cb0ef41Sopenharmony_ci 7171cb0ef41Sopenharmony_ci* `settled` {Function} The [`settled` callback][] to call when a promise 7181cb0ef41Sopenharmony_ci is resolved or rejected. 7191cb0ef41Sopenharmony_ci* Returns: {Function} Call to stop the hook. 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_ci**The `settled` hook must be a plain function. Providing an async function will 7221cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.** 7231cb0ef41Sopenharmony_ci 7241cb0ef41Sopenharmony_ci```mjs 7251cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ciconst stop = promiseHooks.onSettled((promise) => {}); 7281cb0ef41Sopenharmony_ci``` 7291cb0ef41Sopenharmony_ci 7301cb0ef41Sopenharmony_ci```cjs 7311cb0ef41Sopenharmony_ciconst { promiseHooks } = require('node:v8'); 7321cb0ef41Sopenharmony_ci 7331cb0ef41Sopenharmony_ciconst stop = promiseHooks.onSettled((promise) => {}); 7341cb0ef41Sopenharmony_ci``` 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ci### `promiseHooks.onBefore(before)` 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci<!-- YAML 7391cb0ef41Sopenharmony_ciadded: 7401cb0ef41Sopenharmony_ci - v17.1.0 7411cb0ef41Sopenharmony_ci - v16.14.0 7421cb0ef41Sopenharmony_ci--> 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci* `before` {Function} The [`before` callback][] to call before a promise 7451cb0ef41Sopenharmony_ci continuation executes. 7461cb0ef41Sopenharmony_ci* Returns: {Function} Call to stop the hook. 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci**The `before` hook must be a plain function. Providing an async function will 7491cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.** 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci```mjs 7521cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ciconst stop = promiseHooks.onBefore((promise) => {}); 7551cb0ef41Sopenharmony_ci``` 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ci```cjs 7581cb0ef41Sopenharmony_ciconst { promiseHooks } = require('node:v8'); 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ciconst stop = promiseHooks.onBefore((promise) => {}); 7611cb0ef41Sopenharmony_ci``` 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ci### `promiseHooks.onAfter(after)` 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ci<!-- YAML 7661cb0ef41Sopenharmony_ciadded: 7671cb0ef41Sopenharmony_ci - v17.1.0 7681cb0ef41Sopenharmony_ci - v16.14.0 7691cb0ef41Sopenharmony_ci--> 7701cb0ef41Sopenharmony_ci 7711cb0ef41Sopenharmony_ci* `after` {Function} The [`after` callback][] to call after a promise 7721cb0ef41Sopenharmony_ci continuation executes. 7731cb0ef41Sopenharmony_ci* Returns: {Function} Call to stop the hook. 7741cb0ef41Sopenharmony_ci 7751cb0ef41Sopenharmony_ci**The `after` hook must be a plain function. Providing an async function will 7761cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.** 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ci```mjs 7791cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_ciconst stop = promiseHooks.onAfter((promise) => {}); 7821cb0ef41Sopenharmony_ci``` 7831cb0ef41Sopenharmony_ci 7841cb0ef41Sopenharmony_ci```cjs 7851cb0ef41Sopenharmony_ciconst { promiseHooks } = require('node:v8'); 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ciconst stop = promiseHooks.onAfter((promise) => {}); 7881cb0ef41Sopenharmony_ci``` 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci### `promiseHooks.createHook(callbacks)` 7911cb0ef41Sopenharmony_ci 7921cb0ef41Sopenharmony_ci<!-- YAML 7931cb0ef41Sopenharmony_ciadded: 7941cb0ef41Sopenharmony_ci - v17.1.0 7951cb0ef41Sopenharmony_ci - v16.14.0 7961cb0ef41Sopenharmony_ci--> 7971cb0ef41Sopenharmony_ci 7981cb0ef41Sopenharmony_ci* `callbacks` {Object} The [Hook Callbacks][] to register 7991cb0ef41Sopenharmony_ci * `init` {Function} The [`init` callback][]. 8001cb0ef41Sopenharmony_ci * `before` {Function} The [`before` callback][]. 8011cb0ef41Sopenharmony_ci * `after` {Function} The [`after` callback][]. 8021cb0ef41Sopenharmony_ci * `settled` {Function} The [`settled` callback][]. 8031cb0ef41Sopenharmony_ci* Returns: {Function} Used for disabling hooks 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci**The hook callbacks must be plain functions. Providing async functions will 8061cb0ef41Sopenharmony_cithrow as it would produce an infinite microtask loop.** 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ciRegisters functions to be called for different lifetime events of each promise. 8091cb0ef41Sopenharmony_ci 8101cb0ef41Sopenharmony_ciThe callbacks `init()`/`before()`/`after()`/`settled()` are called for the 8111cb0ef41Sopenharmony_cirespective events during a promise's lifetime. 8121cb0ef41Sopenharmony_ci 8131cb0ef41Sopenharmony_ciAll callbacks are optional. For example, if only promise creation needs to 8141cb0ef41Sopenharmony_cibe tracked, then only the `init` callback needs to be passed. The 8151cb0ef41Sopenharmony_cispecifics of all functions that can be passed to `callbacks` is in the 8161cb0ef41Sopenharmony_ci[Hook Callbacks][] section. 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ci```mjs 8191cb0ef41Sopenharmony_ciimport { promiseHooks } from 'node:v8'; 8201cb0ef41Sopenharmony_ci 8211cb0ef41Sopenharmony_ciconst stopAll = promiseHooks.createHook({ 8221cb0ef41Sopenharmony_ci init(promise, parent) {}, 8231cb0ef41Sopenharmony_ci}); 8241cb0ef41Sopenharmony_ci``` 8251cb0ef41Sopenharmony_ci 8261cb0ef41Sopenharmony_ci```cjs 8271cb0ef41Sopenharmony_ciconst { promiseHooks } = require('node:v8'); 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ciconst stopAll = promiseHooks.createHook({ 8301cb0ef41Sopenharmony_ci init(promise, parent) {}, 8311cb0ef41Sopenharmony_ci}); 8321cb0ef41Sopenharmony_ci``` 8331cb0ef41Sopenharmony_ci 8341cb0ef41Sopenharmony_ci### Hook callbacks 8351cb0ef41Sopenharmony_ci 8361cb0ef41Sopenharmony_ciKey events in the lifetime of a promise have been categorized into four areas: 8371cb0ef41Sopenharmony_cicreation of a promise, before/after a continuation handler is called or around 8381cb0ef41Sopenharmony_cian await, and when the promise resolves or rejects. 8391cb0ef41Sopenharmony_ci 8401cb0ef41Sopenharmony_ciWhile these hooks are similar to those of [`async_hooks`][] they lack a 8411cb0ef41Sopenharmony_ci`destroy` hook. Other types of async resources typically represent sockets or 8421cb0ef41Sopenharmony_cifile descriptors which have a distinct "closed" state to express the `destroy` 8431cb0ef41Sopenharmony_cilifecycle event while promises remain usable for as long as code can still 8441cb0ef41Sopenharmony_cireach them. Garbage collection tracking is used to make promises fit into the 8451cb0ef41Sopenharmony_ci`async_hooks` event model, however this tracking is very expensive and they may 8461cb0ef41Sopenharmony_cinot necessarily ever even be garbage collected. 8471cb0ef41Sopenharmony_ci 8481cb0ef41Sopenharmony_ciBecause promises are asynchronous resources whose lifecycle is tracked 8491cb0ef41Sopenharmony_civia the promise hooks mechanism, the `init()`, `before()`, `after()`, and 8501cb0ef41Sopenharmony_ci`settled()` callbacks _must not_ be async functions as they create more 8511cb0ef41Sopenharmony_cipromises which would produce an infinite loop. 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ciWhile this API is used to feed promise events into [`async_hooks`][], the 8541cb0ef41Sopenharmony_ciordering between the two is undefined. Both APIs are multi-tenant 8551cb0ef41Sopenharmony_ciand therefore could produce events in any order relative to each other. 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ci#### `init(promise, parent)` 8581cb0ef41Sopenharmony_ci 8591cb0ef41Sopenharmony_ci* `promise` {Promise} The promise being created. 8601cb0ef41Sopenharmony_ci* `parent` {Promise} The promise continued from, if applicable. 8611cb0ef41Sopenharmony_ci 8621cb0ef41Sopenharmony_ciCalled when a promise is constructed. This _does not_ mean that corresponding 8631cb0ef41Sopenharmony_ci`before`/`after` events will occur, only that the possibility exists. This will 8641cb0ef41Sopenharmony_cihappen if a promise is created without ever getting a continuation. 8651cb0ef41Sopenharmony_ci 8661cb0ef41Sopenharmony_ci#### `before(promise)` 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ci* `promise` {Promise} 8691cb0ef41Sopenharmony_ci 8701cb0ef41Sopenharmony_ciCalled before a promise continuation executes. This can be in the form of 8711cb0ef41Sopenharmony_ci`then()`, `catch()`, or `finally()` handlers or an `await` resuming. 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ciThe `before` callback will be called 0 to N times. The `before` callback 8741cb0ef41Sopenharmony_ciwill typically be called 0 times if no continuation was ever made for the 8751cb0ef41Sopenharmony_cipromise. The `before` callback may be called many times in the case where 8761cb0ef41Sopenharmony_cimany continuations have been made from the same promise. 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_ci#### `after(promise)` 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci* `promise` {Promise} 8811cb0ef41Sopenharmony_ci 8821cb0ef41Sopenharmony_ciCalled immediately after a promise continuation executes. This may be after a 8831cb0ef41Sopenharmony_ci`then()`, `catch()`, or `finally()` handler or before an `await` after another 8841cb0ef41Sopenharmony_ci`await`. 8851cb0ef41Sopenharmony_ci 8861cb0ef41Sopenharmony_ci#### `settled(promise)` 8871cb0ef41Sopenharmony_ci 8881cb0ef41Sopenharmony_ci* `promise` {Promise} 8891cb0ef41Sopenharmony_ci 8901cb0ef41Sopenharmony_ciCalled when the promise receives a resolution or rejection value. This may 8911cb0ef41Sopenharmony_cioccur synchronously in the case of `Promise.resolve()` or `Promise.reject()`. 8921cb0ef41Sopenharmony_ci 8931cb0ef41Sopenharmony_ci## Startup Snapshot API 8941cb0ef41Sopenharmony_ci 8951cb0ef41Sopenharmony_ci<!-- YAML 8961cb0ef41Sopenharmony_ciadded: v18.6.0 8971cb0ef41Sopenharmony_ci--> 8981cb0ef41Sopenharmony_ci 8991cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ciThe `v8.startupSnapshot` interface can be used to add serialization and 9021cb0ef41Sopenharmony_cideserialization hooks for custom startup snapshots. 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ci```console 9051cb0ef41Sopenharmony_ci$ node --snapshot-blob snapshot.blob --build-snapshot entry.js 9061cb0ef41Sopenharmony_ci# This launches a process with the snapshot 9071cb0ef41Sopenharmony_ci$ node --snapshot-blob snapshot.blob 9081cb0ef41Sopenharmony_ci``` 9091cb0ef41Sopenharmony_ci 9101cb0ef41Sopenharmony_ciIn the example above, `entry.js` can use methods from the `v8.startupSnapshot` 9111cb0ef41Sopenharmony_ciinterface to specify how to save information for custom objects in the snapshot 9121cb0ef41Sopenharmony_ciduring serialization and how the information can be used to synchronize these 9131cb0ef41Sopenharmony_ciobjects during deserialization of the snapshot. For example, if the `entry.js` 9141cb0ef41Sopenharmony_cicontains the following script: 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci```cjs 9171cb0ef41Sopenharmony_ci'use strict'; 9181cb0ef41Sopenharmony_ci 9191cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 9201cb0ef41Sopenharmony_ciconst zlib = require('node:zlib'); 9211cb0ef41Sopenharmony_ciconst path = require('node:path'); 9221cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 9231cb0ef41Sopenharmony_ci 9241cb0ef41Sopenharmony_ciconst v8 = require('node:v8'); 9251cb0ef41Sopenharmony_ci 9261cb0ef41Sopenharmony_ciclass BookShelf { 9271cb0ef41Sopenharmony_ci storage = new Map(); 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ci // Reading a series of files from directory and store them into storage. 9301cb0ef41Sopenharmony_ci constructor(directory, books) { 9311cb0ef41Sopenharmony_ci for (const book of books) { 9321cb0ef41Sopenharmony_ci this.storage.set(book, fs.readFileSync(path.join(directory, book))); 9331cb0ef41Sopenharmony_ci } 9341cb0ef41Sopenharmony_ci } 9351cb0ef41Sopenharmony_ci 9361cb0ef41Sopenharmony_ci static compressAll(shelf) { 9371cb0ef41Sopenharmony_ci for (const [ book, content ] of shelf.storage) { 9381cb0ef41Sopenharmony_ci shelf.storage.set(book, zlib.gzipSync(content)); 9391cb0ef41Sopenharmony_ci } 9401cb0ef41Sopenharmony_ci } 9411cb0ef41Sopenharmony_ci 9421cb0ef41Sopenharmony_ci static decompressAll(shelf) { 9431cb0ef41Sopenharmony_ci for (const [ book, content ] of shelf.storage) { 9441cb0ef41Sopenharmony_ci shelf.storage.set(book, zlib.gunzipSync(content)); 9451cb0ef41Sopenharmony_ci } 9461cb0ef41Sopenharmony_ci } 9471cb0ef41Sopenharmony_ci} 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci// __dirname here is where the snapshot script is placed 9501cb0ef41Sopenharmony_ci// during snapshot building time. 9511cb0ef41Sopenharmony_ciconst shelf = new BookShelf(__dirname, [ 9521cb0ef41Sopenharmony_ci 'book1.en_US.txt', 9531cb0ef41Sopenharmony_ci 'book1.es_ES.txt', 9541cb0ef41Sopenharmony_ci 'book2.zh_CN.txt', 9551cb0ef41Sopenharmony_ci]); 9561cb0ef41Sopenharmony_ci 9571cb0ef41Sopenharmony_ciassert(v8.startupSnapshot.isBuildingSnapshot()); 9581cb0ef41Sopenharmony_ci// On snapshot serialization, compress the books to reduce size. 9591cb0ef41Sopenharmony_civ8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf); 9601cb0ef41Sopenharmony_ci// On snapshot deserialization, decompress the books. 9611cb0ef41Sopenharmony_civ8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf); 9621cb0ef41Sopenharmony_civ8.startupSnapshot.setDeserializeMainFunction((shelf) => { 9631cb0ef41Sopenharmony_ci // process.env and process.argv are refreshed during snapshot 9641cb0ef41Sopenharmony_ci // deserialization. 9651cb0ef41Sopenharmony_ci const lang = process.env.BOOK_LANG || 'en_US'; 9661cb0ef41Sopenharmony_ci const book = process.argv[1]; 9671cb0ef41Sopenharmony_ci const name = `${book}.${lang}.txt`; 9681cb0ef41Sopenharmony_ci console.log(shelf.storage.get(name)); 9691cb0ef41Sopenharmony_ci}, shelf); 9701cb0ef41Sopenharmony_ci``` 9711cb0ef41Sopenharmony_ci 9721cb0ef41Sopenharmony_ciThe resulted binary will get print the data deserialized from the snapshot 9731cb0ef41Sopenharmony_ciduring start up, using the refreshed `process.env` and `process.argv` of 9741cb0ef41Sopenharmony_cithe launched process: 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ci```console 9771cb0ef41Sopenharmony_ci$ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1 9781cb0ef41Sopenharmony_ci# Prints content of book1.es_ES.txt deserialized from the snapshot. 9791cb0ef41Sopenharmony_ci``` 9801cb0ef41Sopenharmony_ci 9811cb0ef41Sopenharmony_ciCurrently the application deserialized from a user-land snapshot cannot 9821cb0ef41Sopenharmony_cibe snapshotted again, so these APIs are only available to applications 9831cb0ef41Sopenharmony_cithat are not deserialized from a user-land snapshot. 9841cb0ef41Sopenharmony_ci 9851cb0ef41Sopenharmony_ci### `v8.startupSnapshot.addSerializeCallback(callback[, data])` 9861cb0ef41Sopenharmony_ci 9871cb0ef41Sopenharmony_ci<!-- YAML 9881cb0ef41Sopenharmony_ciadded: v18.6.0 9891cb0ef41Sopenharmony_ci--> 9901cb0ef41Sopenharmony_ci 9911cb0ef41Sopenharmony_ci* `callback` {Function} Callback to be invoked before serialization. 9921cb0ef41Sopenharmony_ci* `data` {any} Optional data that will be passed to the `callback` when it 9931cb0ef41Sopenharmony_ci gets called. 9941cb0ef41Sopenharmony_ci 9951cb0ef41Sopenharmony_ciAdd a callback that will be called when the Node.js instance is about to 9961cb0ef41Sopenharmony_ciget serialized into a snapshot and exit. This can be used to release 9971cb0ef41Sopenharmony_ciresources that should not or cannot be serialized or to convert user data 9981cb0ef41Sopenharmony_ciinto a form more suitable for serialization. 9991cb0ef41Sopenharmony_ci 10001cb0ef41Sopenharmony_ci### `v8.startupSnapshot.addDeserializeCallback(callback[, data])` 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci<!-- YAML 10031cb0ef41Sopenharmony_ciadded: v18.6.0 10041cb0ef41Sopenharmony_ci--> 10051cb0ef41Sopenharmony_ci 10061cb0ef41Sopenharmony_ci* `callback` {Function} Callback to be invoked after the snapshot is 10071cb0ef41Sopenharmony_ci deserialized. 10081cb0ef41Sopenharmony_ci* `data` {any} Optional data that will be passed to the `callback` when it 10091cb0ef41Sopenharmony_ci gets called. 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ciAdd a callback that will be called when the Node.js instance is deserialized 10121cb0ef41Sopenharmony_cifrom a snapshot. The `callback` and the `data` (if provided) will be 10131cb0ef41Sopenharmony_ciserialized into the snapshot, they can be used to re-initialize the state 10141cb0ef41Sopenharmony_ciof the application or to re-acquire resources that the application needs 10151cb0ef41Sopenharmony_ciwhen the application is restarted from the snapshot. 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ci### `v8.startupSnapshot.setDeserializeMainFunction(callback[, data])` 10181cb0ef41Sopenharmony_ci 10191cb0ef41Sopenharmony_ci<!-- YAML 10201cb0ef41Sopenharmony_ciadded: v18.6.0 10211cb0ef41Sopenharmony_ci--> 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci* `callback` {Function} Callback to be invoked as the entry point after the 10241cb0ef41Sopenharmony_ci snapshot is deserialized. 10251cb0ef41Sopenharmony_ci* `data` {any} Optional data that will be passed to the `callback` when it 10261cb0ef41Sopenharmony_ci gets called. 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ciThis sets the entry point of the Node.js application when it is deserialized 10291cb0ef41Sopenharmony_cifrom a snapshot. This can be called only once in the snapshot building 10301cb0ef41Sopenharmony_ciscript. If called, the deserialized application no longer needs an additional 10311cb0ef41Sopenharmony_cientry point script to start up and will simply invoke the callback along with 10321cb0ef41Sopenharmony_cithe deserialized data (if provided), otherwise an entry point script still 10331cb0ef41Sopenharmony_cineeds to be provided to the deserialized application. 10341cb0ef41Sopenharmony_ci 10351cb0ef41Sopenharmony_ci### `v8.startupSnapshot.isBuildingSnapshot()` 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci<!-- YAML 10381cb0ef41Sopenharmony_ciadded: v18.6.0 10391cb0ef41Sopenharmony_ci--> 10401cb0ef41Sopenharmony_ci 10411cb0ef41Sopenharmony_ci* Returns: {boolean} 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ciReturns true if the Node.js instance is run to build a snapshot. 10441cb0ef41Sopenharmony_ci 10451cb0ef41Sopenharmony_ci## Class: `v8.GCProfiler` 10461cb0ef41Sopenharmony_ci 10471cb0ef41Sopenharmony_ci<!-- YAML 10481cb0ef41Sopenharmony_ciadded: v18.15.0 10491cb0ef41Sopenharmony_ci--> 10501cb0ef41Sopenharmony_ci 10511cb0ef41Sopenharmony_ciThis API collects GC data in current thread. 10521cb0ef41Sopenharmony_ci 10531cb0ef41Sopenharmony_ci### `new v8.GCProfiler()` 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ci<!-- YAML 10561cb0ef41Sopenharmony_ciadded: v18.15.0 10571cb0ef41Sopenharmony_ci--> 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ciCreate a new instance of the `v8.GCProfiler` class. 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ci### `profiler.start()` 10621cb0ef41Sopenharmony_ci 10631cb0ef41Sopenharmony_ci<!-- YAML 10641cb0ef41Sopenharmony_ciadded: v18.15.0 10651cb0ef41Sopenharmony_ci--> 10661cb0ef41Sopenharmony_ci 10671cb0ef41Sopenharmony_ciStart collecting GC data. 10681cb0ef41Sopenharmony_ci 10691cb0ef41Sopenharmony_ci### `profiler.stop()` 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ci<!-- YAML 10721cb0ef41Sopenharmony_ciadded: v18.15.0 10731cb0ef41Sopenharmony_ci--> 10741cb0ef41Sopenharmony_ci 10751cb0ef41Sopenharmony_ciStop collecting GC data and return an object.The content of object 10761cb0ef41Sopenharmony_ciis as follows. 10771cb0ef41Sopenharmony_ci 10781cb0ef41Sopenharmony_ci```json 10791cb0ef41Sopenharmony_ci{ 10801cb0ef41Sopenharmony_ci "version": 1, 10811cb0ef41Sopenharmony_ci "startTime": 1674059033862, 10821cb0ef41Sopenharmony_ci "statistics": [ 10831cb0ef41Sopenharmony_ci { 10841cb0ef41Sopenharmony_ci "gcType": "Scavenge", 10851cb0ef41Sopenharmony_ci "beforeGC": { 10861cb0ef41Sopenharmony_ci "heapStatistics": { 10871cb0ef41Sopenharmony_ci "totalHeapSize": 5005312, 10881cb0ef41Sopenharmony_ci "totalHeapSizeExecutable": 524288, 10891cb0ef41Sopenharmony_ci "totalPhysicalSize": 5226496, 10901cb0ef41Sopenharmony_ci "totalAvailableSize": 4341325216, 10911cb0ef41Sopenharmony_ci "totalGlobalHandlesSize": 8192, 10921cb0ef41Sopenharmony_ci "usedGlobalHandlesSize": 2112, 10931cb0ef41Sopenharmony_ci "usedHeapSize": 4883840, 10941cb0ef41Sopenharmony_ci "heapSizeLimit": 4345298944, 10951cb0ef41Sopenharmony_ci "mallocedMemory": 254128, 10961cb0ef41Sopenharmony_ci "externalMemory": 225138, 10971cb0ef41Sopenharmony_ci "peakMallocedMemory": 181760 10981cb0ef41Sopenharmony_ci }, 10991cb0ef41Sopenharmony_ci "heapSpaceStatistics": [ 11001cb0ef41Sopenharmony_ci { 11011cb0ef41Sopenharmony_ci "spaceName": "read_only_space", 11021cb0ef41Sopenharmony_ci "spaceSize": 0, 11031cb0ef41Sopenharmony_ci "spaceUsedSize": 0, 11041cb0ef41Sopenharmony_ci "spaceAvailableSize": 0, 11051cb0ef41Sopenharmony_ci "physicalSpaceSize": 0 11061cb0ef41Sopenharmony_ci } 11071cb0ef41Sopenharmony_ci ] 11081cb0ef41Sopenharmony_ci }, 11091cb0ef41Sopenharmony_ci "cost": 1574.14, 11101cb0ef41Sopenharmony_ci "afterGC": { 11111cb0ef41Sopenharmony_ci "heapStatistics": { 11121cb0ef41Sopenharmony_ci "totalHeapSize": 6053888, 11131cb0ef41Sopenharmony_ci "totalHeapSizeExecutable": 524288, 11141cb0ef41Sopenharmony_ci "totalPhysicalSize": 5500928, 11151cb0ef41Sopenharmony_ci "totalAvailableSize": 4341101384, 11161cb0ef41Sopenharmony_ci "totalGlobalHandlesSize": 8192, 11171cb0ef41Sopenharmony_ci "usedGlobalHandlesSize": 2112, 11181cb0ef41Sopenharmony_ci "usedHeapSize": 4059096, 11191cb0ef41Sopenharmony_ci "heapSizeLimit": 4345298944, 11201cb0ef41Sopenharmony_ci "mallocedMemory": 254128, 11211cb0ef41Sopenharmony_ci "externalMemory": 225138, 11221cb0ef41Sopenharmony_ci "peakMallocedMemory": 181760 11231cb0ef41Sopenharmony_ci }, 11241cb0ef41Sopenharmony_ci "heapSpaceStatistics": [ 11251cb0ef41Sopenharmony_ci { 11261cb0ef41Sopenharmony_ci "spaceName": "read_only_space", 11271cb0ef41Sopenharmony_ci "spaceSize": 0, 11281cb0ef41Sopenharmony_ci "spaceUsedSize": 0, 11291cb0ef41Sopenharmony_ci "spaceAvailableSize": 0, 11301cb0ef41Sopenharmony_ci "physicalSpaceSize": 0 11311cb0ef41Sopenharmony_ci } 11321cb0ef41Sopenharmony_ci ] 11331cb0ef41Sopenharmony_ci } 11341cb0ef41Sopenharmony_ci } 11351cb0ef41Sopenharmony_ci ], 11361cb0ef41Sopenharmony_ci "endTime": 1674059036865 11371cb0ef41Sopenharmony_ci} 11381cb0ef41Sopenharmony_ci``` 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_ciHere's an example. 11411cb0ef41Sopenharmony_ci 11421cb0ef41Sopenharmony_ci```js 11431cb0ef41Sopenharmony_ciconst { GCProfiler } = require('v8'); 11441cb0ef41Sopenharmony_ciconst profiler = new GCProfiler(); 11451cb0ef41Sopenharmony_ciprofiler.start(); 11461cb0ef41Sopenharmony_cisetTimeout(() => { 11471cb0ef41Sopenharmony_ci console.log(profiler.stop()); 11481cb0ef41Sopenharmony_ci}, 1000); 11491cb0ef41Sopenharmony_ci``` 11501cb0ef41Sopenharmony_ci 11511cb0ef41Sopenharmony_ci[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm 11521cb0ef41Sopenharmony_ci[Hook Callbacks]: #hook-callbacks 11531cb0ef41Sopenharmony_ci[V8]: https://developers.google.com/v8/ 11541cb0ef41Sopenharmony_ci[`--heapsnapshot-near-heap-limit`]: cli.md#--heapsnapshot-near-heap-limitmax_count 11551cb0ef41Sopenharmony_ci[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage 11561cb0ef41Sopenharmony_ci[`Buffer`]: buffer.md 11571cb0ef41Sopenharmony_ci[`DefaultDeserializer`]: #class-v8defaultdeserializer 11581cb0ef41Sopenharmony_ci[`DefaultSerializer`]: #class-v8defaultserializer 11591cb0ef41Sopenharmony_ci[`Deserializer`]: #class-v8deserializer 11601cb0ef41Sopenharmony_ci[`ERR_BUFFER_TOO_LARGE`]: errors.md#err_buffer_too_large 11611cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error 11621cb0ef41Sopenharmony_ci[`GetHeapCodeAndMetadataStatistics`]: https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#a6079122af17612ef54ef3348ce170866 11631cb0ef41Sopenharmony_ci[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4 11641cb0ef41Sopenharmony_ci[`NODE_V8_COVERAGE`]: cli.md#node_v8_coveragedir 11651cb0ef41Sopenharmony_ci[`Serializer`]: #class-v8serializer 11661cb0ef41Sopenharmony_ci[`after` callback]: #afterpromise 11671cb0ef41Sopenharmony_ci[`async_hooks`]: async_hooks.md 11681cb0ef41Sopenharmony_ci[`before` callback]: #beforepromise 11691cb0ef41Sopenharmony_ci[`buffer.constants.MAX_LENGTH`]: buffer.md#bufferconstantsmax_length 11701cb0ef41Sopenharmony_ci[`deserializer._readHostObject()`]: #deserializer_readhostobject 11711cb0ef41Sopenharmony_ci[`deserializer.transferArrayBuffer()`]: #deserializertransferarraybufferid-arraybuffer 11721cb0ef41Sopenharmony_ci[`init` callback]: #initpromise-parent 11731cb0ef41Sopenharmony_ci[`serialize()`]: #v8serializevalue 11741cb0ef41Sopenharmony_ci[`serializer._getSharedArrayBufferId()`]: #serializer_getsharedarraybufferidsharedarraybuffer 11751cb0ef41Sopenharmony_ci[`serializer._writeHostObject()`]: #serializer_writehostobjectobject 11761cb0ef41Sopenharmony_ci[`serializer.releaseBuffer()`]: #serializerreleasebuffer 11771cb0ef41Sopenharmony_ci[`serializer.transferArrayBuffer()`]: #serializertransferarraybufferid-arraybuffer 11781cb0ef41Sopenharmony_ci[`serializer.writeRawBytes()`]: #serializerwriterawbytesbuffer 11791cb0ef41Sopenharmony_ci[`settled` callback]: #settledpromise 11801cb0ef41Sopenharmony_ci[`v8.stopCoverage()`]: #v8stopcoverage 11811cb0ef41Sopenharmony_ci[`v8.takeCoverage()`]: #v8takecoverage 11821cb0ef41Sopenharmony_ci[`vm.Script`]: vm.md#new-vmscriptcode-options 11831cb0ef41Sopenharmony_ci[worker threads]: worker_threads.md 1184