News for hackers. Stuff that matters.
Re:Copying objects in JavaScript by Scarbutt on Tuesday November 13, 2018 @12:05PM (#18442571)
Another option if you can afford it is to just use immutablejs.

Or make your functions return new objects.

Re:Copying objects in JavaScript by realharo on Tuesday November 13, 2018 @01:10PM (#18443198)
I would recommend immer (https://github.com/mweststrate/immer) instead of ImmutableJS. You can work with regular JS objects, plus it plays much nicer with TypeScript.
Re:Copying objects in JavaScript by benvan on Tuesday November 13, 2018 @04:06PM (#18444904)
I wrote a tiny library called fn-update for this that does composable functional updates (https://github.com/benvan/fn-update).

Personally, I prefer not having to mutate data, even wrapped in a produce method

Re:Copying objects in JavaScript by realharo on Tuesday November 13, 2018 @07:17PM (#18446156)
Having to specify the path using an array of strings would be an instant dealbreaker for me. It messes with all tooling and is bad for readability.
Re:Copying objects in JavaScript by nine_k on Tuesday November 13, 2018 @12:18PM (#18442676)
I suppose immutable.js supports partial reuse of objects, that is, if you only change the value of one attribute, the changed copy has this attribute set differently, but the rest is shallow-copied?

If so, indeed immutable objects would not run into the problem of copying, as long as you can afford them to be immutable. (That is, you're not working with any APIs that assume and use mutability.)

Re:Copying objects in JavaScript by TheAceOfHearts on Tuesday November 13, 2018 @05:15PM (#18445436)
ImmutableJS is a pretty huge library. I'd conjecture most web apps don't have a legitimate need for something so comprehensive and would be better off with a simpler solution.

If you know the shapes of your objects ahead of time you can create one-off functions, which will probably be faster and require far less code.

Re:Copying objects in JavaScript by beaconstudios on Tuesday November 13, 2018 @12:18PM (#18442682)
I cannot recommend ramda enough. It provides the immutability and flexibility of immutablejs, but because it's build in a functional paradigm, the logic is fully composeable so complex, deeply nested changes are very simple and readable.
Re:Copying objects in JavaScript by oweiler on Tuesday November 13, 2018 @12:38PM (#18442871)
That is an apples to oranges comparison. ramda does not provide persistent data structures.
Re:Copying objects in JavaScript by beaconstudios on Tuesday November 13, 2018 @12:59PM (#18443092)
both are used to approach the same problem: immutable manipulation of javascript data. The fact that immutableJS provides a persistent object is an implementation detail in the strategy used to solve that problem.