Live data from Hacker News

Which is the best method for deep cloning in JavaScript?

medium.com

51–60 of 62 posts

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

#51

Earlier quoted context omitted.

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 clear intention of the JSON spec is that JSON numbers should deserialise to doubles, and this is how every decoder that I've ever seen handles it. 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.

It was intentionally written to be very simple and achieved that goal. If your opinion is that that’s a poor choice, sure. But that’s an opinion, not a fact. Specifying a specific float format would cripple an interchange format and I think that would be a mistake. The intention here is to allow each origin and destination to decide how to fit the generic data into their representation formats.

The spec is not ambiguous. And a spec is a spec is a spec. You can implement it or not. But you can’t just decide “Enh… they didn’t mean it like that so it’s wrong to satisfy the spec. You should really satisfy it wrongly.”

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

#52

Earlier quoted context omitted.

Cloning is fairly common when you're writing functional style code with immutable objects. Not "code smell".

But when you’re using immutable data, deep cloning is not something you need for to worry about. So it kind of is an anti-pattern in this case.

Copy-on-write is a common operation on immutable data and deep cloning is how you do that in JavaScript in a safe manner.

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

#53

I'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.

Have you used React? You must copy every complex object or list everytime before you update the state.

s/React/Redux? (for example)

With setState in React, you don't necessarily have to copy to update the state. setState merges the partial object you provide with the current state.

https://reactjs.org/docs/state-and-lifecycle.html#state-upda...

In Redux reducers, however, you need a full copy.

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

#54

As far as I know there is no way in JavaScript to copy a function with its closure, so fundamentally writing a perfect cloning algorithm in JavaScript is not possible.

> copy a function with its closure

By closure, do you mean the values of free variables at the time of the copy?

If so, the inability to do a perfect copy is applicable to regular functions at the top level, too. (For such functions, other global variables are free variables.)

Considering this, I think this is why copying a function is generally defined as just having a reference to the function that can later be used to invoke it. It does not include each copy of the function having its own copy of the function's free variables from the instant of the copy. Separate invocations of the multiple copies (i.e. references) of the function may affect the shared closure environment/free variables.

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

#55

I'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.

I've avoided writing objects altogther. Never ran across a problem I needed them for.

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

#56
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.

Consider the Date object harmful, use ISO strings instead, and problem solved.

So how do you manipulate ISO dates as strings?

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

#57

Earlier quoted context omitted.

But when you’re using immutable data, deep cloning is not something you need for to worry about. So it kind of is an anti-pattern in this case.

Copy-on-write is a common operation on immutable data and deep cloning is how you do that in JavaScript in a safe manner.

[deleted]

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

#58

Earlier quoted context omitted.

Consider the Date object harmful, use ISO strings instead, and problem solved.

So how do you manipulate ISO dates as strings?

Although ISO date strings can be fairly trivial to read and manipulate with RexExp or a custom parser, depending on the complexity of the task, I would recommend using something like iso-fns.

https://iso-fns.org

It's too bad this library (and approach) never took off, but it's there to use nevertheless.

Even without iso-fns, it's not as if mid level developer can't figure out how to write a function to perform a specific operation on an ISO date string.

After over 10 years of web development, I do think this is the best known approach and is even better than storing UNIX time. Even if there were no existing libraries to help with manipulating ISO strings, I will gladly take the inconvenience in exchange for the rest of the advantages.

With ISO strings, you don't get any behavioral quirks from Date; they are totally compatible with JSON; you can store the time zone along with the time; there is nothing to stop you from changing the time zone while leaving all the other values intact; ISO strings also support durations.

Basically, they support most of what a software developer will be expected to do with times and dates but without any behavior or assumptions about the client time zone that introduce bugs like with Date. `` also uses ISO strings out of the box, which can be really convenient, and values from `` can be easily appended into an actual ISO string.

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

#59

Earlier quoted context omitted.

But when you’re using immutable data, deep cloning is not something you need for to worry about. So it kind of is an anti-pattern in this case.

Copy-on-write is a common operation on immutable data and deep cloning is how you do that in JavaScript in a safe manner.

Oh yeah, that’s true. I was thinking of RRB trees, hash array mapped tries and things like this.

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

#60

Earlier quoted context omitted.

So how do you manipulate ISO dates as strings?

Although ISO date strings can be fairly trivial to read and manipulate with RexExp or a custom parser, depending on the complexity of the task, I would recommend using something like iso-fns. https://iso-fns.org It's too bad this library (and approach) never took off, but it's there to use nevertheless. Even without iso-fns, it's not as if mid level developer can't figure out how to write a function to perform a spec…

[deleted]
Post reply on HN