Node.js – v8.serializer.writeValue() Method


The v8.serializer.writeValue() method is used for writing the values to the internal buffer passed in the input parameter. This method serializes the JavaScript value and adds its serialized form to the internal buffer. An error is thrown if the value can’t be serialized.

Syntax

v8.serializer.writeValue(value)

Parameters

  • value − The value can be of any type. This is the value that will be serialized and written to the internal buffer.

Example 1

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

// v8.serializer.writeValue() example

// Importing the v8 module
const v8 = require('v8');
const serializer = new v8.Serializer();

// Writing value to the internal buffer
console.log(serializer.writeValue("Welcome to TutorialsPoint !"));
// Printing value by releasing buffer
console.log(serializer.releaseBuffer());

Output

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

Example 2

Let’s have a look at one more example

// v8.serializer.writeValue() example

// Importing the v8 module
const v8 = require('v8');
const serializer = new v8.Serializer();

// Writing value to the internal buffer
console.log(serializer.writeValue("Welcome to TutorialsPoint !"));
console.log(serializer.writeValue("Hello user #1"));

// Printing value by releasing buffer
console.log(serializer.releaseBuffer());
// Value which was written again was appended into the internal buffer
// which is why you receive different buffer this time

Output

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

Updated on: 17-Aug-2021

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements