This rule flags code that uses JSON.parse(JSON.stringify()) or library functions like _.cloneDeep() to create deep clones of objects.

Why is this an issue?

structuredClone() is the modern, standardized way to create deep clones in JavaScript. It’s part of the web platform and provides several advantages over alternative approaches.

The JSON.parse(JSON.stringify()) pattern has significant limitations. It cannot handle functions, undefined values, symbols, or Date objects correctly. It also fails with circular references and loses prototype information.

Library-based solutions like _.cloneDeep() work correctly but add external dependencies to your project. They increase bundle size and create maintenance overhead.

structuredClone() handles these cases properly. It supports a wide range of data types including Date objects, RegExp, Maps, Sets, and even handles circular references. It’s also faster than JSON-based approaches and doesn’t require external libraries.

What is the potential impact?

Using inferior cloning methods can lead to subtle bugs when the cloned objects don’t behave as expected. The JSON approach can silently corrupt data by dropping properties or changing types. External libraries increase bundle size and create dependency management overhead.

How to fix?

Replace JSON.parse(JSON.stringify()) with structuredClone(). This provides proper deep cloning without the limitations of JSON serialization.

Non-compliant code example

const clone = JSON.parse(JSON.stringify(originalObject)); // Noncompliant

Compliant code example

const clone = structuredClone(originalObject);

Documentation