I only clone data using JSON.parse(JSON.stringify(someThing)), and thus avoid all the mess that comes with trying to clone anything else than primitives types.
This fails, among other scenarios, if `someThing` includes a Date object. >>> const z = { d: new Date() } undefined >>> typeof z.d "object" >>> const a = JSON.parse(JSON.stringify(z)) undefined >>> typeof a.d "string" >>> After the roundtrip, the property `d` is now a string, not a Date object.
Which is the best method for deep cloning in JavaScript?
41–50 of 62 posts
Re: Which is the best method for deep cloning in JavaScript?
#42I only clone data using JSON.parse(JSON.stringify(someThing)), and thus avoid all the mess that comes with trying to clone anything else than primitives types.
I sure hope `someThing` is never `undefined`, for your sake.
Re: Which is the best method for deep cloning in JavaScript?
#43I've been programming JavaScript for ~10 years now (wtf) and I've yet to encounter a case where I needed to "deep clone" an object. It has always been a code smell indicative of a need for some other structural refactoring.
Re: Which is the best method for deep cloning in JavaScript?
#44Right out of the gate they mention that the first approach listed doesn't clone Symbol...and then they treat this as a flaw. I haven't used Symbol in JS much, but I was under the impression that it's _supposed_ to be a sorta "interned" value (in the sense that there exists exactly one instance in existence of each Symbol value)...which would mean that cloning them isn't a concept that makes sense. My loose awareness…
This article has a pretty good overview of what symbols are, usually a corner case, but good to know -
https://medium.com/intrinsic-blog/javascript-symbols-but-why...
Re: Which is the best method for deep cloning in JavaScript?
#45I've been programming JavaScript for ~10 years now (wtf) and I've yet to encounter a case where I needed to "deep clone" an object. It has always been a code smell indicative of a need for some other structural refactoring.
Re: Which is the best method for deep cloning in JavaScript?
#46I'll take a fast deep clone for my case over one that handles all cases.
Re: Which is the best method for deep cloning in JavaScript?
#47But I just want to point out that cloning isn’t an end in itself.
The article includes a lot of judgements but it’s all context-free. Whether a clone method is good or not depends on its suitability for a purpose. E.g., whether you want non-enumerable properties cloned (if you care at all) really depends on your specific uses. Not to mention performance is very often a concern and that isn’t covered here.
Re: Which is the best method for deep cloning in JavaScript?
#48Earlier quoted context omitted.
This is because inf and nan were left out of the json spec, right? Great format
Right because the spec is not IEEE 754. JSON numbers are not floats. They’re numbers with as many decimal points as you want. It’s up to the serializer/deserializer to decide how to handle them.
The first decoder was the JS eval() function, so JSON was clearly intended to be a subset of JS. Ambiguity in the spec is not a licence to deviate from JS semantics, it just means that the spec is poorly written.
Re: Which is the best method for deep cloning in JavaScript?
#49Personally I just don't try to clone anything that isn't JSON-valid data, and I usually use the stringify/parse strategy. It's slow in a hot loop, but it's fast enough for the rare situations where I need to fully clone something. Most of the time, I can just use readonly types and avoid defensively cloning the whole tree. When I want to replace part of an immutable object, destructuring works fine
Re: Which is the best method for deep cloning in JavaScript?
#50I've been programming JavaScript for ~10 years now (wtf) and I've yet to encounter a case where I needed to "deep clone" an object. It has always been a code smell indicative of a need for some other structural refactoring.
Cloning is fairly common when you're writing functional style code with immutable objects. Not "code smell".