JavaScript Interview Question #148
What is structuredClone()?
Generators, Browser Storage, Security & Advanced Runtime Concepts Senior Advanced
Quick Interview Answer
structuredClone creates a deep clone of values supported by the structured clone algorithm. It can handle many values that JSON serialization does not preserve correctly, including circular references, Map, Set, Date, and various typed data structures.
Detailed Explanation
structuredClone creates a deep clone of values supported by the structured clone algorithm. It can handle many values that JSON serialization does not preserve correctly, including circular references, Map, Set, Date, and various typed data structures.
It does not clone every possible JavaScript value; for example, functions cannot be cloned with structuredClone. Developers should still understand the supported types and whether cloning is actually necessary for the design.
Code Example
const original = {
created: new Date(),
tags: new Set(["js", "web"])
};
original.self = original;
const copy = structuredClone(original);
console.log(copy !== original); // true
console.log(copy.created instanceof Date); // true
console.log(copy.self === copy); // true