Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readJavaScript

structuredClone vs JSON copy: what breaks

Compare what JSON copying and structuredClone do with dates, collections, cycles, functions and class instances.

You copied an object with JSON.parse(JSON.stringify(x)) and the copy's date is now a string. The Set and the Map came back as empty objects. Nothing threw, so the bug surfaced later, far from the copy.

What the JSON round trip does

JSON.stringify converts valid dates to ISO strings and serializes ordinary maps and sets as empty objects. It omits function and undefined properties in objects and writes null for those values in arrays.

const original = {
  when: new Date('2025-07-16T09:00:00Z'),
  tags: new Set(['a', 'b']),
  scores: new Map([['x', 1]]),
  note: undefined,
  run() {},
}
const copy = JSON.parse(JSON.stringify(original))
console.log(copy)
// { when: '2025-07-16T09:00:00.000Z', tags: {}, scores: {} }

const loop = { name: 'loop' }
loop.self = loop
JSON.parse(JSON.stringify(loop))
// TypeError: Converting circular structure to JSON

The date is a string, tags and scores are empty, note and run are missing, and the self-referencing object throws.

Copy with structuredClone

structuredClone is a global function, so there is nothing to import. It keeps the types that JSON throws away. I gave it the same object, without the function, plus a typed array and a cycle.

const original = {
  when: new Date('2025-07-16T09:00:00Z'),
  tags: new Set(['a', 'b']),
  scores: new Map([['x', 1]]),
  bytes: new Uint8Array([1, 2, 3]),
  note: undefined,
}
original.self = original

const copy = structuredClone(original)
console.log(copy)
// <ref *1> {
//   when: 2025-07-16T09:00:00.000Z,
//   tags: Set(2) { 'a', 'b' },
//   scores: Map(1) { 'x' => 1 },
//   bytes: Uint8Array(3) [ 1, 2, 3 ],
//   note: undefined,
//   self: [Circular *1]
// }
console.log(copy.when instanceof Date, copy.self === copy, copy !== original)
// true true true

The date is still a Date, the collections still work, and self in the copy points at the copy.

What structuredClone refuses

A function cannot be cloned, so structuredClone throws instead of skipping it. An ordinary class instance clones its enumerable own string-keyed properties and loses its prototype.

try {
  structuredClone({ run() {} })
} catch (error) {
  console.log(error.name) // DataCloneError
}

class Point {
  constructor(x) { this.x = x }
  double() { return this.x * 2 }
}
const p = structuredClone(new Point(2))
console.log(p, p instanceof Point, typeof p.double)
// { x: 2 } false undefined

The error is a DataCloneError, and the clone of Point is a plain object with x and no double. Object.assign(new Point(0), p) gives you a real Point again, and double() returns 4.

I reach for structuredClone on plain data that holds dates, maps or sets. Anything with methods I rebuild from the cloned fields.