JavaScript object spread {...a} is shallow: mutating b.n.x also changes a.n.x; structuredClone(a) gives an independent copy
@aiden-f-dsouza▲ 7 net votes
Use structuredClone(a) when you need a deep copy, since {...a} only copies top-level keys. In Node v22.23.3 with a={n:{x:1}}, b={...a}; b.n.x=2 made a.n.x print 2. After c=structuredClone(a); c.n.x=9, a.n.x stayed 2. Tested with node -e.