structuredClone and Transferable Objects: Copying Data Across JavaScript Boundaries
Understand deep cloning, circular references, unsupported values, and when moving a buffer is better than copying it.
structuredClone() copies supported JavaScript values, including cyclic object graphs. Its transfer option can move supported transferable resources such as ArrayBuffers, avoiding a copy while making the original resource unusable.
THE PRODUCTION PROBLEM STATEMENT
JSON serialization is often used as a quick deep-copy trick, but it drops or transforms values and fails on circular references. Indiscriminate cloning can duplicate large buffers and add avoidable memory pressure.
Clone structured data rather than round-tripping JSON
structuredClone preserves cycles and supports several built-in data types that JSON does not represent directly. It is useful at ownership boundaries such as worker messaging, IndexedDB, and isolated snapshots.
It is not a universal object copier. Functions and DOM nodes cannot be cloned, and custom prototype chains, property descriptors, getters, and setters are not preserved as class behavior.
const source = { name: 'report' };
source.self = source;
const copy = structuredClone(source);
console.log(copy !== source); // true
console.log(copy.self === copy); // trueTransfer large buffers when ownership can move
A transferable can be moved instead of copied when the sender no longer needs it. The receiver gets the resource and the sender’s ArrayBuffer is detached. This can save substantial copying for large binary messages.
Treat transfer as an ownership handoff. Any view backed by the buffer can no longer be used as before, so transfer only after the sender has finished reading or mutating it.
const bytes = new Uint8Array(largeFileBytes);
worker.postMessage({ bytes }, [bytes.buffer]);
// The original backing buffer has been transferred.
console.log(bytes.byteLength);Choose clone, transfer, or shared memory deliberately
Cloning is a useful default for modest messages because both sides retain independent values. Transfer fits large exclusive payloads. Shared memory is a separate concurrency design with synchronization requirements; introduce it only when the measured workload justifies it.
A DataCloneError usually means part of a message is unsupported. Keep messages data-only and include enough request identity to associate results with the work that produced them.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. It copies supported data but does not preserve custom prototype chains or methods as class behavior.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Multi-Threaded JavaScript on the Web: Web Workers, SharedArrayBuffer, and Atomics Synchronization
A guide to true parallelism in the browser: zero-copy memory sharing, thread synchronization with Atomics.wait and Atomics.notify, and cross-origin isolation.
Advanced Memory Management in JavaScript: WeakRef, FinalizationRegistry, and Leak Detection
How to build ephemeral caches, observe garbage collection cycles, and diagnose detached DOM references in V8.