News for hackers. Stuff that matters.
Re:Deep Cloning Objects in JavaScript, the Modern Way by quickthrower2 on Thursday December 21, 2023 @10:26PM (#38730627)
If something is immutable then you never need to clone it. Just copy the reference.

If an object is immutable and you want to copy/mutate one item it then you can surgically clone it in O(how deeply nested the item to change is). Object spread being the simple case.

JS makes this messy to do though! Lots of spreads and array maps or you pull in a library like Immer and now have “coloured” objects. Immutability in JS is not idiomatic so for that reason I try to avoid unless I really need to.

There are no comments on this comment yet.