No, there is no build in way to deep clone objects.

And deep cloning is a difficult and edgey thing to deal with.

Lets assume that a method deepClone(a) should return a "deep clone" of b.

Now a "deep clone" is an object with the same [[Prototype]] and having all the own properties cloned over.

For each clone property that is cloned over, if that has own properties that can be cloned over then do so, recursively.

Of course were keeping the meta data attached to properties like [[Writable]] and [[Enumerable]] in-tact. And we will just return the thing if it's not an object.

Copyvar deepClone = function (obj) {
    try {
        var names = Object.getOwnPropertyNames(obj);
    } catch (e) {
        if (e.message.indexOf("not an object") > -1) {
            // is not object
            return obj;
        }    
    }
    var proto = Object.getPrototypeOf(obj);
    var clone = Object.create(proto);
    names.forEach(function (name) {
        var pd = Object.getOwnPropertyDescriptor(obj, name);
        if (pd.value) {
            pd.value = deepClone(pd.value);
        }
        Object.defineProperty(clone, name, pd);
    });
    return clone;
};

This will fail for a lot of edge cases.

Live Example

As you can see you can't deep clone objects generally without breaking their special properties (like .length in array). To fix that you have to treat Array seperately, and then treat every special object seperately.

What do you expect to happen when you do deepClone(document.getElementById("foobar")) ?

As an aside, shallow clones are easy.

CopyObject.getOwnPropertyDescriptors = function (obj) {
    var ret = {};
    Object.getOwnPropertyNames(obj).forEach(function (name) {
        ret[name] = Object.getOwnPropertyDescriptor(obj, name);
    });
    return ret;
};

var shallowClone = function (obj) {
    return Object.create(
        Object.getPrototypeOf(obj),
        Object.getOwnPropertyDescriptors(obj)
    );
};
Answer from Raynos on Stack Overflow
Top answer
1 of 4
18

No, there is no build in way to deep clone objects.

And deep cloning is a difficult and edgey thing to deal with.

Lets assume that a method deepClone(a) should return a "deep clone" of b.

Now a "deep clone" is an object with the same [[Prototype]] and having all the own properties cloned over.

For each clone property that is cloned over, if that has own properties that can be cloned over then do so, recursively.

Of course were keeping the meta data attached to properties like [[Writable]] and [[Enumerable]] in-tact. And we will just return the thing if it's not an object.

Copyvar deepClone = function (obj) {
    try {
        var names = Object.getOwnPropertyNames(obj);
    } catch (e) {
        if (e.message.indexOf("not an object") > -1) {
            // is not object
            return obj;
        }    
    }
    var proto = Object.getPrototypeOf(obj);
    var clone = Object.create(proto);
    names.forEach(function (name) {
        var pd = Object.getOwnPropertyDescriptor(obj, name);
        if (pd.value) {
            pd.value = deepClone(pd.value);
        }
        Object.defineProperty(clone, name, pd);
    });
    return clone;
};

This will fail for a lot of edge cases.

Live Example

As you can see you can't deep clone objects generally without breaking their special properties (like .length in array). To fix that you have to treat Array seperately, and then treat every special object seperately.

What do you expect to happen when you do deepClone(document.getElementById("foobar")) ?

As an aside, shallow clones are easy.

CopyObject.getOwnPropertyDescriptors = function (obj) {
    var ret = {};
    Object.getOwnPropertyNames(obj).forEach(function (name) {
        ret[name] = Object.getOwnPropertyDescriptor(obj, name);
    });
    return ret;
};

var shallowClone = function (obj) {
    return Object.create(
        Object.getPrototypeOf(obj),
        Object.getOwnPropertyDescriptors(obj)
    );
};
2 of 4
17

The 2022 solution for this is to use structuredClone

See : https://developer.mozilla.org/en-US/docs/Web/API/structuredClone

CopystructuredClone(x)
Top answer
1 of 2
41

You should use the library json2.js. It is the basis for the standard JSON.stringify(...) that some browsers include natively.

You can find the page it originated from here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

The script automatically makes sure it only adds a JSON.stringify(...) method if it doesn't already exist so there is no danger including it in a browser that has it.

2 of 2
0

Depending on your use case, you can use a serialization library.

This is helpful when you want to store (or transfer) more complex Javascript objects without them breaking when you want to parse them back later (among other uses),

However, you won't get a standard JSON file but rather a superset of it that likely can only be used by the same library.

I've listed some of the reasons why you might want to consider a Serializer library instead of JSON.stringify() and JSON.parse(). Every Serializer is different but ideally, a Serializer can:

  1. Handle circular references, which JSON.parse() cannot handle.
  2. Handle all data types including Date, Error, undefined, or Functions - which JSON.stringify can't.
  3. Handle the conversion of non-standard data types.
  4. Deal with much larger objects.
  5. Handle Unicode or other special characters better.

Here's a library that I found easy to use: https://github.com/erossignon/serialijse

As an example use case for Serialization (although there are more prominent use cases and this is just how I used it), I used the minified version of the mentioned library to write a Developer Tools console code snippet to backup all the configurations and storage of a browser extension into a file to restore later. This would not have been possible with JSON.stringify() due to the custom types the extensions had stored in their local storage.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › what-is-difference-between-json-parse-and-json-stringify-methods-in-javascript
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ? - GeeksforGeeks
July 23, 2025 - It contrasts JSON.parse(). With replacer parameters, logic on key-value pairs is feasible. Date formats aren't allowed in JSON; thus, they should be included as strings. JSON.stringify(value, replacer, space); Example: This example we converts the JavaScript object myInfo into a JSON string using JSON.stringify().
🌐
Medium
medium.com › @pmzubar › why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521
Why use JSON “parse” and “stringify” methods a bad practice to clone objects in JavaScript (2023 update) | by Petro Zubar | Medium
October 10, 2023 - Cloning the objects often becomes a problem for JS programmers. Surprisingly, the second most popular answer to the question `What is the most efficient way to deep clone an object in JavaScript?` is… to JSON.parse(JSON.stringify()) it!
🌐
Reddit
reddit.com › r/developersindia › alternative of json.parse(json.stringify(data)) as its a synchronous function
r/developersIndia on Reddit: Alternative of JSON.parse(JSON.stringify(data)) as its a synchronous function
December 21, 2023 -

https://www.linkedin.com/posts/arunm-engineer_one-line-caused-the-entire-company-10x-down-activity-7178235801720303616-2f3p?utm_source=share&utm_medium=member_ios

If this is actually an issue, whats a better alternative? Will doing async await work?

🌐
GitHub
github.com › fastify › fast-json-stringify
GitHub - fastify/fast-json-stringify: 2x faster than JSON.stringify() · GitHub
fast-json-stringify is significantly faster than JSON.stringify() for small payloads.
Author   fastify
🌐
Joe Attardi
joeattardi.dev › customizing-jsonparse-and-jsonstringify
Customizing `JSON.parse` and `JSON.stringify`
February 15, 2025 - This function is similar to the reviver function for JSON.parse. It is called first with the object itself, then is called again for each key/value pair. By the time it is called with the key/value pairs, the stringification of these properties has already been done.
🌐
GitHub
github.com › aikoven › json-stringifier
GitHub - aikoven/json-stringifier: Alternative to JSON.stringify() that supports altering the behavior of the stringification process at string level
However, there's no way to achieve this using built-in JSON.stringify() function: its replacer parameter only allows substituting serialized values, but not resulting strings. stringify() function provided by this library accepts a stringifier parameter that lets us override the stringification of values in the object tree.
Author   aikoven
Find elsewhere
🌐
Medium
dinesh-rawat.medium.com › why-you-should-avoid-json-parse-json-stringify-for-cloning-objects-in-javascript-8337d6761926
Why you should avoid JSON.parse(JSON.stringify()) for cloning objects in JavaScript | by Dinesh Rawat | Medium
March 29, 2023 - 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:
🌐
GitHub
github.com › josdejong › lossless-json
GitHub - josdejong/lossless-json: Parse JSON without risk of losing numeric information
The difference is that lossless-json preserves information of big numbers. lossless-json parses numeric values not as a regular number but as a LosslessNumber, a lightweight class which stores the numeric value as a string.
Starred by 464 users
Forked by 33 users
Languages   TypeScript 92.4% | JavaScript 6.3% | HTML 1.3%
🌐
Zhenghao
zhenghao.io › posts › json-oddities
JSON and the stringification oddities in JavaScript
One way we can make our own implementation of JSON.stringify faster is to have the user provide a schema of the object so we know the object structure before serialization. This can save us a ton of work. In fact, many JSON.stringify-alternative libraries are implemented this way to make serialization faster.
Top answer
1 of 2
13

Take a closer look at the two comments you've put onto the question:

I suppose after re-reading my question, I'm seeing that in my C# example, I'm serializing the XML and THEN converting the serialized object ToString();. There inlies the rub.

and

I guess for continuity, it would be better (for me) to have a method that looks like this... JSON.serialize(obj).toString(); or jsonObject().toString();... this way it would look much like my C#... but now I'm over complicating it.

Now remember that in Javascript, an object is a hash (rather, if using Prototype or another framework, it should be qualified as a "special kind of hash" - but the simple form works for this example):

var obj = {
   foo: 1,
   bar: function() { console.log(this.foo); }
}
obj.foo; // 1
obj.bar; // function reference
obj.bar(); // function call
obj['foo']; // 1
obj['bar']; // function reference
obj['bar'](); // function call

The only reason a serialize() might be necessary in Javascript is to cut out the functions, references to other objects, etc.

So, to go back to your C# example - we've just cut out .Serialize() as unnecessary. An object is a hash, it's already serialized, further "serialization" would have to be done manually in your own code anyway. All that leaves you is .ToString().

Does .stringify() make more sense now?

2 of 2
12

This is because that JSON notation was specified in 1999 not after 2002 (asp.net is released at that year). so i guess they didn't know about the serialize.

Jokes apart,

Hearing the word serialization, first thing that comes to my mind is like converting the data to bytes, here JSON.stringify makes perfect sense as it converts the object to a string representation not a byte representation.

PS:

@Chase Florell, you can't just add JSON.serialize, as in strict mode, this code may actually fail in some browsers.

as JSON is not your average Object.

🌐
npm
npmjs.com › package › fast-json-stringify
fast-json-stringify - npm
fast-json-stringify is significantly faster than JSON.stringify() for small payloads.
      » npm install fast-json-stringify
    
Published   Feb 06, 2026
Version   6.3.0
Author   Matteo Collina
🌐
Medium
medium.com › @ronaldssebalamu › json-parse-vs-json-stringify-ad2d3218bc44
JSON.parse() vs JSON.stringify(). Today we are yet to delve into one of… | by Ronald Ssebalamu | Medium
September 13, 2023 - Conclusion JSON.parse() is used to convert JSON data into a JavaScript object , to make it accessible and modifiable with in the JavaScript code. JSON.stringify() is used to convert a JavaScript object into a JSON formatted string, that is usually used to send data onto the server, or transmitting it onto the network.
🌐
GitHub
gist.github.com › izy521 › 4d394dec28054d54684269d91b16cb8a
`JSON.parse( JSON.stringify( obj) )` has been regarded as the fastest method for deep copying Objects, but is it? This is mainly just to test. Obviously Functions aren't allowed in JSON. · GitHub
`JSON.parse( JSON.stringify( obj) )` has been regarded as the fastest method for deep copying Objects, but is it? This is mainly just to test. Obviously Functions aren't allowed in JSON. - objectdeepcopy.js
🌐
Online String Tools
onlinestringtools.com › json-parse-string
JSON Unstringify a String – Online String Tools
This function is the reverse of JSON.stringify() and therefore it's sometimes called JSON unstringify. Make sure the input string is in quotes and it's a valid JSON string, or else you will get a syntax error.