ReviseAlgo Logo

ES6+ Modern JavaScript

Structured Clone & structuredClone()

Master deep copying objects in JavaScript. Learn the structuredClone API, cloning rules, supported data types, and limitations.

Last Updated: July 15, 2026 10 min read

1. Introduction

Historically, copying objects in JavaScript was limited to shallow copies or hacky serialization workarounds. Modern browsers and Node.js runtimes provide the structuredClone() global function to create deep copies of objects and arrays safely.

2. Why It Matters

Using the spread operator (...) or Object.assign() only copies properties shallowly. If the source object contains nested objects, modifying properties in the copied object will mutate the original object. The structuredClone() API recursively clones all nested properties, creating a true deep copy.

3. Real-World Analogy

Think of Copying a Blueprint Folder:

  • Shallow Copy (Photocopying index pages): You photocopy the cover sheet of the folder. The sheet contains bookmarks pointing to the original reference files in the filing cabinet. If you modify a reference file (mutate a nested object property), the changes are visible to anyone reading the photocopy.
  • JSON parsing hack (Redrawing sketches): Scanning the pages to black-and-white PDF and re-printing them. It creates a copy, but you lose detailed drawings (like Date objects or RegExp rules).
  • structuredClone (Complete replication): A 3D scanner that duplicates the folder and all its contents, including nested pages and drawings (Dates, Arrays, Maps, and Sets), creating a completely independent copy.

4. Deep Copying with structuredClone()

The global structuredClone() function accepts a value (object, array, map, set, or primitive) and returns a deep copy:

5. Supported vs Unsupported Types

Supported Types (Cloned correctly) Unsupported Types (Throws DataCloneError)
Plain objects, Arrays, Maps, Sets, Dates, RegExp objects, Blobs, Files, ArrayBuffers Functions, DOM nodes, Symbol properties, getter/setter properties, Proxy objects

6. Practical Example

This script demonstrates cloning an object that contains circular references (objects referencing each other), which would crash custom recursive cloning scripts:

7. Common Mistakes

  • Trying to clone objects containing functions: Functions cannot be cloned using the structured clone algorithm. Calling structuredClone({ greet() {} }) throws a DataCloneError.
  • Assuming prototype chains are preserved: structuredClone() discards prototype links. If you clone an instance of a custom class (e.g. new User()), the returned object will be a plain JavaScript object, losing the class methods.

8. Quick Quiz

Q1: What happens if you attempt to copy an object containing a function using structuredClone()?

A) The function is copied as a reference

B) It throws a DataCloneError exception

Answer: B — The structured clone algorithm does not support functions, throwing a DataCloneError if you attempt to clone them.

9. Scenario-Based Challenge

The JSON.parse deep-copy cleanup:

An older codebase deep-copies state objects using JSON.parse(JSON.stringify(state)). This works for plain objects but discards Date fields, converting them to ISO strings. Refactor this deep copy utility to use structuredClone().

10. Debugging Exercise

Explain why this test assertion crashes, and how to fix it:

class UserAccount {
  constructor(name) { this.name = name; }
  sayHello() { return `Hello ${this.name}`; }
}

const user = new UserAccount('Alice'); const clonedUser = structuredClone(user);

console.log(clonedUser.sayHello()); // crashes with TypeError! Why?

View Solution

Diagnosis: The structuredClone() function discards prototype links during copying. The returned clonedUser is a plain object { name: "Alice" }, which does not have the sayHello() method on its prototype chain, throwing a TypeError.

Fix: Create a new instance of the class manually, or restore the prototype link of the cloned object using Object.setPrototypeOf():

const clonedUser = structuredClone(user);
Object.setPrototypeOf(clonedUser, UserAccount.prototype); // Restore prototype link
console.log(clonedUser.sayHello()); // "Hello Alice"

11. Interview Questions

🟢 Q1: Compare structuredClone() with the JSON serialization hack (JSON.parse(JSON.stringify(obj))) for deep copying.

Answer:
JSON Hack: Discards non-JSON data types (converting Date objects to strings, and throwing away RegExp, Map, Set, or Blob objects). It also crashes if the object contains circular references.
structuredClone(): Natively copies complex types (Dates, RegExp, Maps, Sets, and binary arrays) and handles circular references automatically. It is also faster than parsing JSON strings.

12. Production Considerations

  • Browser Support: structuredClone() is supported in all modern browsers and Node.js (version 17+). For older environments, include a polyfill (like core-js) in your build pipeline.