Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6752755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:04:55+00:00 2026-05-26T13:04:55+00:00

What’s the fastest alternative to JSON.parse(JSON.stringify(x)) There must be a nicer/built-in way to perform

  • 0

What’s the fastest alternative to

JSON.parse(JSON.stringify(x))

There must be a nicer/built-in way to perform a deep clone on objects/arrays, but I haven’t found it yet.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:04:56+00:00Added an answer on May 26, 2026 at 1:04 pm

    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.

    var 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.

    Object.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)
        );
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.