~2 دقیقه مطالعه • بروزرسانی ۹ دی ۱۴۰۴
1. Introduction
The node:v8 module provides APIs that expose internal functionality of the V8 engine. These tools are used for memory analysis, profiling, debugging, and building advanced developer tooling.
2. Accessing the Module
const v8 = require('node:v8');
3. Cached Data Compatibility
3.1 v8.cachedDataVersionTag()
Returns a version tag indicating whether cached vm.Script data is compatible with the current V8 instance.
console.log(v8.cachedDataVersionTag());
4. Heap & Memory Statistics
4.1 v8.getHeapCodeStatistics()
Returns statistics about code and metadata stored in the heap.
4.2 v8.getHeapSpaceStatistics()
Returns statistics for each V8 heap space (new_space, old_space, code_space, etc.).
4.3 v8.getHeapStatistics()
Provides detailed heap metrics including:
- total_heap_size
- used_heap_size
- heap_size_limit
- malloced_memory
- native_contexts
- detached_contexts
- external_memory
4.4 v8.getCppHeapStatistics()
Returns memory statistics for the C++ heap (CppHeap), with brief or detailed modes.
5. Heap Snapshots
5.1 v8.getHeapSnapshot()
Generates a JSON heap snapshot stream compatible with Chrome DevTools.
v8.getHeapSnapshot().pipe(process.stdout);
5.2 v8.writeHeapSnapshot()
Writes a heap snapshot to disk.
const filename = v8.writeHeapSnapshot();
Important Notes:
- Heap snapshots require ~2× the heap size in memory.
- Snapshot generation is synchronous and blocks the event loop.
- Snapshots are isolate-specific (workers are separate).
6. Coverage Tools
6.1 v8.takeCoverage()
Writes coverage data to the directory specified by NODE_V8_COVERAGE.
6.2 v8.stopCoverage()
Stops coverage collection and frees related memory.
7. V8 Flags
7.1 v8.setFlagsFromString()
Programmatically sets V8 command-line flags.
v8.setFlagsFromString('--trace_gc');
8. Querying Objects in the Heap
8.1 v8.queryObjects()
Searches the heap for objects whose prototype chain includes a given constructor.
class A {}
console.log(v8.queryObjects(A));
9. Serialization API
9.1 v8.serialize()
Serializes a JavaScript value using the structured clone algorithm.
const buf = v8.serialize({ a: 1 });
9.2 v8.deserialize()
Deserializes a buffer created by serialize().
const obj = v8.deserialize(buf);
9.3 Serializer / Deserializer Classes
Provide fine-grained control over serialization behavior.
10. Promise Hooks
Track the lifecycle of promises using four events:
- init
- before
- after
- settled
const { promiseHooks } = require('node:v8');
promiseHooks.onInit((promise, parent) => {});
11. Practical Use Cases
- Memory leak detection
- Heap profiling
- Building DevTools integrations
- Coverage collection in CI pipelines
- Structured data serialization
- Promise lifecycle tracking
Conclusion
The node:v8 module provides powerful low-level access to the V8 engine, enabling deep memory analysis, profiling, coverage tracking, and serialization. These APIs are essential for advanced Node.js development, performance engineering, and tooling. With proper use, they offer unparalleled insight into the internal behavior of JavaScript execution within Node.js.
نوشته و پژوهش شده توسط دکتر شاهین صیامی