Deep cloning
structuredClone()
Baseline widely available
Chrome98
Edge98
Firefox94
Safari15.4
Features it needs
- structuredClone()Widely available
These packages walk an object graph by hand, copying nested arrays and objects one property at a time so the copy shares no references with the original. A global function now does the same job: structuredClone() copies arrays, plain objects, Maps, Sets, and dates in one call, no walking required.
When this applies
Deep-copying plain data such as arrays, objects, Maps, Sets, and dates.
The native approach
const copy = structuredClone(original);When the dependency is still right
An answer that always says "the platform covers it" is worse than no answer. These are the cases where this one does not hold.
- You need to clone functions or DOM nodes. structuredClone throws a DataCloneError on both, where lodash's version drops what it cannot handle and carries on.
- You clone class instances and need the prototype back. structuredClone does not throw here, which is the trap: it returns a plain object with the same fields and no methods, so the failure shows up later at the first method call.
- You need to clone something containing a value structuredClone doesn't support, such as an Error's custom properties beyond message and name.
- You clone large objects on a hot path and measured the difference. Being faster than structuredClone on plain data is the whole pitch of rfdc and fast-copy, and a benchmark on your own shapes is the only way to settle it.