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.
I sure hope `someThing` is never `undefined`, for your sake.
Which is the best method for deep cloning in JavaScript?
11–20 of 62 posts
Re: Which is the best method for deep cloning in JavaScript?
#12https://developer.mozilla.org/en-US/docs/Web/API/structuredC...
Re: Which is the best method for deep cloning in JavaScript?
#13I 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.
Which is the first method they mention and just flat out tell to avoid it. "cloneJSON is very slow and can’t do much. Please avoid it." I mean, I also use it since forever. But I might look into the winner cloneLib. If it is really faster, it might be worth it, but I likely still won't trust it for anything complex.
Re: Which is the best method for deep cloning in JavaScript?
#14It may look like for simple use cases the JSON one works. But one thing not included in this page is that there are things it just doesn't serialize, even when it's actual data: const native = { number: Number.POSITIVE_INFINITY }; const cloned = JSON.parse(JSON.stringify(native)); // unlike NaN, we can compare Number.POSITIVE_INFINITY to itself console.log(native.number === native.number); // but it doesn't serialize…
Re: Which is the best method for deep cloning in JavaScript?
#15I 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.
>>> 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.Re: Which is the best method for deep cloning in JavaScript?
#16I 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.
Re: Which is the best method for deep cloning in JavaScript?
#17 > In other languages like Java, each class is expected to implement its own clone method
This is incorrect. There is an `Object.clone()` in Java, but it's not implemented by default for most types, implementing it is fraught with complexity, and the standard book of Java advice strongly recommends avoiding it. If you indicate that your type supports cloning (using the marker interface `Cloneable`), you still only get a shallow copy by default.Re: Which is the best method for deep cloning in JavaScript?
#18Re: Which is the best method for deep cloning in JavaScript?
#19Earlier quoted context omitted.
I sure hope `someThing` is never `undefined`, for your sake.
That seems to be a logical thing to ensure has been validated before this code is run, so no need to complicate the simple example in a HN post?
is an odd way to suffix code that will throw a SyntaxError when handed a primitive type.
Re: Which is the best method for deep cloning in JavaScript?
#20Earlier quoted context omitted.
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.
All the data comes from a JSON API and thus doesn't contain Date objects to begin with.