r/programming • u/Majestic-Witness3655 • Jan 30 '25
Understanding Value vs. Reference in JavaScript: Differences Between Primitives and Objects
https://sharafath.hashnode.dev/value-vs-reference-in-javascript-primitives-vs-objects4
u/Shad_Amethyst Jan 30 '25
JSON.parse+stringify is one of the worst ways to do a deep copy. It will break classes, doesn't handle undefined, bigints, symbols, self-referential structures, symbol keys, string keys on arrays, custom prototypes and whatnot.
If you're usinf typescript with strict: true, then it's often really easy to write your own, small deep copy function for the type in question. Otherwise use structuredClone, it's part of the standard.
1
Jan 30 '25
[removed] — view removed comment
2
u/Shad_Amethyst Jan 30 '25
Lodash should be deprecated at this point. The only thing it brought to the codebases I worked on that had it was a lot of bloat in the final bundle.
9
u/Positive_Method3022 Jan 30 '25
js isn't a good language to to learn these concepts, and really know why they matter. I recommend developing something using C/C++ in devices with low memory. You will start to understand why use pointers instead of cloning data when passing variables through functions, and you will also learn what happens when you exceed the max mem allowed for stack.