Friday 27 September 2024

What is the most efficient way to deep clone an object in JavaScript?

 

1. Native Deep Cloning with structuredClone

The most efficient and now widely supported way to deep clone an object is by using the native structuredClone() function. This method works in all major browsers and Node.js (version 17 and above).

const original = { a: 1, b: { c: 2 } };
const clone = structuredClone(original);

console.log(clone); // { a: 1, b: { c: 2 } }
Read more »

Labels: