Serialization in Worker threads [ Node.js ]

Aditya Yadav
3 min readSep 23, 2024

--

Serialization refers to the process of converting data structures or objects into a format that can be easily stored or transmitted and then reconstructed later. In the context of message passing between the main thread and worker threads in Node.js, serialization involves converting JavaScript objects, arrays, and other data types into a format that can be transferred between the two threads, and then deserialized back into the original data structure.

Why Serialization is Needed in Message Passing

When using postMessage() to send data between the main thread and worker threads, the data is transferred between two separate execution contexts. These contexts may not share memory directly (unlike with SharedArrayBuffer). Therefore, the data must be serialized (converted into a transferable format) and deserialized (reconstructed) at the other end.

This allows complex data structures, such as objects or arrays, to be transferred safely between threads.

Serialization Example

Here’s an example of serialization and deserialization using postMessage:

Main Thread (main.js)

const { Worker } = require('worker_threads');

// Create an object to send to the worker
const dataToSend = { name: "Alice", age: 30, skills: ["JavaScript", "Node.js"] };

// Create a worker and send the object
const worker = new Worker('./worker.js');
worker.postMessage(dataToSend);

// Receive the modified data from the worker
worker.on('message', (message) => {
console.log('Main thread received:', message);
});

Worker Thread (worker.js)

const { parentPort } = require('worker_threads');

// Listen for messages from the main thread
parentPort.on('message', (data) => {
console.log('Worker received:', data);

// Modify the object
data.skills.push('WorkerThreads');

// Send the modified object back to the main thread
parentPort.postMessage(data);
});

How Serialization Works Here:

  1. The main thread sends an object (dataToSend) using postMessage().
  2. Node.js serializes the object (converts it into a format that can be sent to another thread).
  3. The worker thread receives the serialized data, which is automatically deserialized back into a JavaScript object.
  4. The worker modifies the object and sends it back to the main thread. This involves another round of serialization and deserialization.

JSON as a Common Serialization Format

// Example of serialization and deserialization
const object = { name: "Alice", age: 30 };

// Serialize to JSON string
const serialized = JSON.stringify(object);
console.log(serialized); // Output: '{"name":"Alice","age":30}'

// Deserialize back to JavaScript object
const deserialized = JSON.parse(serialized);
console.log(deserialized); // Output: { name: 'Alice', age: 30 }

Performance Impact of Serialization

  • Serialization adds overhead when passing large or complex data between threads. This is because each time data is passed, it must be serialized, sent to the other thread, and deserialized on the receiving end.
  • In Node.js, the postMessage() function performs this serialization/deserialization automatically using structured clone algorithm, which is more advanced and faster than JSON for more complex objects.

Complex Data Types and Serialization

Some data types may not be serialized in a straightforward manner:

  • Functions and closures cannot be serialized using postMessage(). If you attempt to send a function, it will be stripped out.
  • Certain complex objects, such as DOM elements or RegExp, require special handling or cannot be transferred.
  • For shared memory without serialization overhead, SharedArrayBuffer or ArrayBuffer can be used.

Summary of Serialization in Message Passing

  • Serialization is the process of converting data into a transferable format.
  • In Node.js, message passing between threads involves serializing and deserializing data to ensure safe transmission.
  • postMessage automatically handles this for you, but it does add overhead, especially for large

--

--

Aditya Yadav
Aditya Yadav

Written by Aditya Yadav

Software Engineer who talks about tech concepts in web development

No responses yet