Javascript Structuredclone Vs JSON Deep Copy Map Set Date Circular
Definisi
structuredClone() (native, Node 17+/browser modern) membuat deep copy sejati yang mendukung Map, Set, Date, ArrayBuffer, dan reference siklik. JSON.parse(JSON.stringify()) hanya menangani data JSON murni (gagal pada Map/Set/Date/undefined/fungsi).
Kapan dipakai
Pakai structuredClone untuk menyalin state kompleks sebelum mutasi (agar original tidak berubah). Hindari JSON trick untuk data non-JSON — akan kehilangan tipe.
Cara kerja
structuredClone
const orig = { map: new Map([['a', 1]]), date: new Date(), nested: { x: 1 } };
const copy = structuredClone