Why structuredClone is the Best Choice for Deep Copying in JavaScript?
November 5, 2024
If you've ever interviewed for a frontend position, you've probably been asked about deep copying objects in JavaScript. It's one of those classic interview questions, like explaining the difference between shallow and deep copies, or writing your own deep copy function from scratch. (If you're preparing for interviews, you might want to check out our article on implementing shallow and deep copies.)
In the JavaScript development world, one common challenge developers face is creating copies of complex data structures. This article explores the concept of deep cloning and examines why the new structuredClone
method is becoming the preferred solution for modern JavaScript applications.
To understand deep cloning, consider this example:
const calendarEvent = {
title: "ExpalinThis",
date: new Date(123),
attendees: ["PJ", "Li", "Lynn"],
};
const copied = structuredClone(calendarEvent);
// The copied object has identical properties but is completely independent
copied.attendees; // ["PJ", "Li", "Lynn"]
copied.date; // Date: Wed Dec 31 1969 16:00:00
calendarEvent.attendees === copied.attendees; // false
Now, there are several ways to create deep copies in JavaScript. The old-school approach uses JSON.parse(JSON.stringify(obj))
. Another option is Lodash's _.cloneDeep(obj)
. And now we have the native structuredClone(obj)
. But why should you prefer structuredClone
?
Historically, developers had several options for deep cloning:
- Using
JSON.parse(JSON.stringify(obj))
: This method, while common, has significant limitations. It cannot handle JavaScript native classes or complex data structures likeSet
. For instance, if your object contains aSet
, it would be incorrectly converted to an empty object{}
. - Using Lodash's
_.cloneDeep(obj)
: While this method effectively handles various data types, it comes with a cost. Importing just the cloneDeep function adds 17.4KB (5.3KB gzipped) to your application. Importing the entire Lodash library is even more expensive at 71.5KB (25.2KB gzipped). - Using the new
structuredClone()
: This native JavaScript function provides efficient deep cloning without additional dependencies.
The advantages of structuredClone
include:
- No additional library dependencies (zero bundle size added)
- Native browser support in Chrome, Edge, Safari
- Native runtime support in Node.js and Deno
- Efficient handling of complex data structures
However, structuredClone
does have some limitations. It cannot clone:
- Functions as object keys
- DOM nodes as values
- Certain specialized objects that throw
DataCloneError
To sum up,
structuredClone
is now the recommended method for deep cloning in JavaScript due to its native implementation and zero dependencies.- While alternatives like Lodash's
cloneDeep
work well, they add unnecessary weight to your application. - Unless you need to support specific edge cases or worker environments,
structuredClone
should be your first choice for deep cloning operations in modern JavaScript development. - Before implementing any cloning solution, consider your specific use case and requirements, particularly regarding supported data types and browser compatibility needs.