{"success":true,"name":"know-shallow-vs-deep-copy-before-duplicating-javascript-obje","description":"JS code copies an object or array before modifying the copy.","category":"ai-brimind","path":"ai-brimind/know-shallow-vs-deep-copy-before-duplicating-javascript-obje/SKILL.md","file":"SKILL.md","content":"---\nname: know-shallow-vs-deep-copy-before-duplicating-javascript-obje\ndescription: \"JS code copies an object or array before modifying the copy.\"\n---\n\n# Know Shallow vs Deep Copy Before Duplicating JavaScript Objects\n\n## When to use\nProject is JavaScript, and code duplicates data (spread, Object.assign, slice) intending to change the copy without affecting the original.\n\n## Example request\nSpread copy of an object, then editing copy.items.push(...) → the original's items changed too; spread copies one level only — shallow copy of nested data. 'Backup' of state saved by assignment (backup = state) → that's the same object, not a copy; the backup mutates with the original — reference vs copy confusion. JSON.parse(JSON.stringify(x)) used on data with dates → dates become strings silently — lossy deep-copy trick.\n\n## Instructions\nAct like a developer who always asks 'copy of the box, or copy of everything inside the box?'. Before duplicating data: (1) know the three levels — assignment (=) copies nothing, it's a second name for the same object; spread/Object.assign/slice copy ONE level, nested objects inside are still shared references; a deep copy duplicates everything; (2) choose based on what will be MUTATED: if only top-level fields change, shallow is fine and cheap; if anything nested will be modified, you need a deep copy — use structuredClone() (the modern built-in) rather than the JSON round-trip, which silently corrupts dates, undefined, and functions; (3) better yet, question the mutation itself — creating a NEW object with the changed values (immutable update pattern) often removes the need for deep copying entirely. Verify the classic symptom: 'I changed X and Y changed too' means a shared reference survived your copy. Only proceed once you can state which level of copy each duplication performs and why that level matches what gets mutated.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1102\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}