structuredClone, not JSON.parse(JSON.stringify())
The JSON round trip drops undefined, turns dates into strings, loses Map and Set, and throws on cycles. structuredClone copies all of those correctly, and every runtime has it built in now.
const copy = structuredClone(state);It still won't copy functions or the prototypes of class instances. For plain data, it's the deep copy you actually meant.
javascript