Node.js – v8.serialize() Method


The v8.serialize() method provides a method to serialize JavaScript values in a way that is compatible and therefore can be used with HTML structured clone algorithm. The format is also backward compatible. Equal or same JavaScript objects may result in different serialized outputs.

Syntax

v8.serialize(value)

Parameters

It takes a single parameter −

  • value − The input parameter that needs to be serialized.

The function returns a buffer output after serializing the input value.

Example 1

Create a file with the name "serialize.js" and copy the following code snippet. After creating the file, use the command "node serialize.js" to run this code.

// v8.serialize() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Initializing the data variable
var data = "Welcome to Tutorials Point !"

// Printing the serialized value
console.log(v8.serialize(data));

Output

C:\home
ode>> node serialize.js <Buffer ff 0d 22 1c 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 20 50 6f 69 6e 74 20 21>

Example 2

Let’s have a look at one more example

// v8.serialize() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Initializing the data variable
var data = "Welcome to Tutorials Point !"

// Printing the serialized value
console.log('Serializing data', v8.serialize(data));

console.log('Serializing numbers', v8.serialize(12345))

console.log('Serializing decimal value', v8.serialize(12345.543))

Output

C:\home
ode>> node serialize.js Serializing data <Buffer ff 0d 22 1c 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 20 50 6f 69 6e 74 20 21> Serializing numbers <Buffer ff 0d 49 f2 c0 01> Serializing decimal value <Buffer ff 0d 4e dd 24 06 81 c5 1c c8 40>

Updated on: 18-Aug-2021

818 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements