4 ms·
Slightly related: I die a little inside each time I see `JSON.parse(JSON.stringify(object))` thinking about the inefficiencies involved compared to how you'd do
by seanwilson 1y ago
Slightly related: I die a little inside each time I see `JSON.parse(JSON.stringify(object))` thinking about the inefficiencies involved compared to how you'd do this in a more efficient language.
There's structuredClone (https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone https://developer.mozilla.org/en-US/docs/Web/API/Window/stru... https://caniuse.com/?search=structuredClone https://caniuse.com/?search=structuredClone) with baseline support (93% of users), but it doesn't work if fields contain DOM objects or functions meaning you might have to iterate over and preprocess objects before cloning so more error-prone, manual and again inefficient?
- remify 1y agoMarch 2022 is not that long ago for a codebase. It takes time but Javascript has come a long way and it's definitely going in the right direction.
- mary-ext 1y agoalso depends on the codebase, if you use frameworks with deep reactivity like Vue, you can't do structuredClone without toRaw (which only works if the object is shallow) as it'd throw on proxy objects. Svelte has `$state.snapshot()` for this reason I believe.