Live data from Hacker News

Which is the best method for deep cloning in JavaScript?

medium.com

11–20 of 62 posts

Re: Which is the best method for deep cloning in JavaScript?

#11
post #4

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.

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?

Re: Which is the best method for deep cloning in JavaScript?

#13

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.

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.

Emphasis on _data_. I don't have a need to clone complex objects (what the article is trying to do). The data I need to clone are usually small so when it comes to performance it's negligible, not worth adding another dependency to the project.

Re: Which is the best method for deep cloning in JavaScript?

#14

It 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…

This is because inf and nan were left out of the json spec, right? Great format

Re: Which is the best method for deep cloning in JavaScript?

#15

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.

Re: Which is the best method for deep cloning in JavaScript?

#16
post #15

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.

All the data comes from a JSON API and thus doesn't contain Date objects to begin with.

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?

#19
post #4

Earlier 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?

> avoid all the mess that comes with trying to clone anything else than primitives types

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?

#20
post #15

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

[deleted]
Post reply on HN