Node.js Reference ยท node:sqlite and Runtime Services
Node.js v8.serialize() and v8.deserialize()
Reference for v8.serialize() and v8.deserialize() in Node.js: encode Maps, Sets, Dates, typed arrays and cycles to a Buffer, with limits compared to JSON.
v8.serialize() encodes a JavaScript value into a Buffer using V8's own structured clone format, and v8.deserialize() reads it back. Unlike JSON, the format understands Map, Set, Date, RegExp, BigInt, typed arrays, undefined, NaN, sparse arrays and circular references.
It is handy for caches, inter-process messages and files that only Node.js reads. It is the same format used by worker_threads messages and by child_process with serialization: 'advanced'.
import v8 from 'node:v8'; const bytes = v8.serialize({ when: new Date(0), tags: new Set(['a']) }); console.log(v8.deserialize(bytes));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
v8.serialize(value): Buffer v8.deserialize(buffer): any
| Name | Description |
|---|---|
value |
Any structured-cloneable value. |
buffer |
A Buffer, TypedArray or DataView containing serialized data. |
Returns: serialize() returns a Buffer; deserialize() returns a new value, never the original object.
Errors: serialize() throws an Error ("... could not be cloned") for functions, symbols and objects with unsupported internals. deserialize() throws an Error for corrupt or truncated input. (structuredClone() throws a DataCloneError for the same values; v8.serialize() throws a plain Error.)
Usage notes
- Versioned format. The binary format can change between V8 versions. Do not store serialized data for a long time or share it between very different Node.js versions; use JSON or a schema format for long-term storage.
- Never deserialize untrusted input. The format is not designed as a safe wire format. Treat data from other machines or users as hostile and parse it with JSON and validation instead.
- Prototypes and class identity are lost. Instances become plain objects. Rebuild classes yourself after
deserialize(). - Deep copies.
v8.deserialize(v8.serialize(x))was a popular deep clone, butstructuredClone()does the same without the detour through a buffer. - Size. Binary encoding is usually smaller than JSON for numbers and typed arrays, and slower to inspect by eye.
- Classes for streaming.
v8.Serializerandv8.Deserializerlet you write several values to one buffer and customize host objects, at the cost of more code. - Buffers and typed arrays are copied into the output; large ones make large buffers.