Skip to Content
Multithreaded Javascript
book

Multithreaded Javascript

by Thomas Hunter, Bryan English
September 2021
Intermediate to advanced
211 pages
5h 32m
English
O'Reilly Media, Inc.
Content preview from Multithreaded Javascript

Appendix. Structured Clone Algorithm

The structured clone algorithm is a mechanism that JavaScript engines use when copying objects using certain APIs. Most notably, it’s used when passing data between workers, though other APIs use it as well. With this mechanism, data is serialized and then later deserialized as an object inside another JavaScript realm.

When objects are cloned in this manner, such as from the main thread to a worker thread or from one worker to another, modifying an object on one side will not affect the object on the other side. There are essentially two copies of the data now. The purpose of the structured clone algorithm is to provide a friendlier mechanism for developers than what JSON.stringify does, while imposing reasonable limitations.

Browsers use the structured clone algorithm when copying data between web workers. Node.js, similarly, uses it when copying data between worker threads. Basically, when you see a .postMessage() call, data being passed in is cloned in this way. Browsers and Node.js follow the same rules, but they each support additional object instances that can be copied.

As a quick rule of thumb, any data that can be cleanly represented as JSON can be safely cloned in this manner. Sticking to data represented in this manner will certainly lead to very few surprises. That said, the structured clone algorithm supports several other types of data as well.

First off, all of the primitive data types available in JavaScript, with the exception ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Clean Code in JavaScript

Clean Code in JavaScript

James Padolsey
TypeScript Cookbook

TypeScript Cookbook

Stefan Baumgartner

Publisher Resources

ISBN: 9781098104429Errata Page