Why you should avoid JSON.parse(JSON.stringify()) for cloning objects in JavaScript

Dinesh Rawat
3 min readMar 29, 2023

When it comes to cloning objects in JavaScript, developers have several options at their disposal. While some of these methods are straightforward and reliable, others can lead to unexpected issues or performance problems.

Why you should avoid JSON.parse(JSON.stringify()) for cloning objects in JavaScript

One cloning technique that has gained popularity in recent years is using JSON.parse(JSON.stringify()) to create a deep copy of an object. This method is simple and easy to implement, but it has several limitations and potential drawbacks that developers should be aware of. In this blog post, we will explore why JSON.parse(JSON.stringify()) is a bad practice for cloning objects in JavaScript, and what alternatives should be used instead.

Here are some reasons why its a bad practice:

Loss of Functionality

JSON.stringify() and JSON.parse() are intended for data serialization and deserialization. They do not handle all data types that can be present in an object in JavaScript. For example, functions, regular expressions, and undefined values cannot be serialized, and will therefore be lost during the serialization process. When the object is deserialized with JSON.parse(), any methods or functions attached to the original object will be lost, and undefined values will be converted to null.

Performance Overhead

Serializing an object into a JSON string, and then parsing it back into a new object, can be a relatively slow process compared to other cloning methods. This is because the object must be converted to a string, and then parsed back into a new object. This can lead to performance issues, especially when working with large or complex objects.

Inability to Clone Circular References:

JSON.stringify() cannot handle circular references, which are objects that contain references to themselves. When JSON.stringify() is used on an object with circular references, it will throw an error. This makes it unsuitable for cloning objects that contain circular references.

Immutable Data

When you clone an object with JSON.parse(JSON.stringify()), the resulting object is a new instance, but it will not share any references with the original object. This means that any changes made to the original object will not be reflected in the cloned object. The cloned object will be immutable and cannot be modified directly.

💁 Breaking the habit: exploring other object cloning techniques in JavaScript

There are several ways to clone an object in JavaScript that are more reliable and efficient than using JSON.parse(JSON.stringify()). Here are some alternatives:

The spread operator

The spread operator (…) can be used to create a shallow copy of an object. This method is fast and easy to use, but it only creates a shallow copy, which means that any nested objects or arrays will still be references to the original object.

For example:

const originalObj = {a: 1, b: 2}
const clonedObj = {...originalObj};

Object.assign()

Object.assign() can be used to create a shallow copy of an object. It allows you to merge the properties of multiple objects into a new object. Like the spread operator, Object.assign() only creates a shallow copy, so any nested objects or arrays will still be references to the original object.

For example:

const originalObj = {a: 1, b: 2}
const clonedObj = Object.assign({}, originalObj);

Deep cloning libraries

For more complex objects or arrays that contain nested objects or arrays, you can use deep cloning libraries such as Lodash or Underscore. These libraries provide functions that create a deep copy of an object or array, including any nested objects or arrays.

For example:

const originalObj = {a: 1, b: {c: 2}}
const clonedObj = _.cloneDeep(originalObj); // Using Lodash

It’s important to choose the appropriate cloning method based on the complexity of the object you want to clone, as well as your performance requirements.

— -

To summarize, while JSON.parse(JSON.stringify()) can be used to clone simple objects that do not contain functions, regular expressions, undefined values, or circular references, it is not a suitable solution for cloning more complex objects. Other cloning methods, such as the spread operator, Object.assign(), or deep cloning libraries, should be used instead.

#JavaScript #ObjectCloning #Performance #WebDevelopment #Programming

--

--

Dinesh Rawat

Seasoned software engineer, Content creator, Helping teams achieve their goals. https://www.linkedin.com/in/dinesh-rawat/