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 is built into every runtime now.
const copy = structuredClone(state);It still will not copy functions or class instances' prototypes. For plain data it is the deep copy you meant.
javascript