Dev.to AI 🤖 Ai 👁 2 📖 2 min read

JSON.parse(JSON.stringify()) vs structuredClone: What Survives the Copy

I copied objects with JSON.parse(JSON.stringify(obj)) for years. It works right up until the object holds something JSON has no idea about. I ran a test on Node.js 22 with four values that break the trick: const orig

I copied objects with JSON.parse(JSON.stringify(obj)) for years. It works right up until the object holds something JSON has no idea about.

I ran a test on Node.js 22 with four values that break the trick:

const original = {
  date: new Date("2026-01-01T00:00:00Z"),
  map: new Map([["a", 1]]),
  undef: undefined,
  nan: NaN,
};

const viaJson = JSON.parse(JSON.stringify(original));
console.log(viaJson);
console.log(Object.keys(viaJson));

Output:

{ date: '2026-01-01T00:00:00.000Z', map: {}, nan: null }
[ 'date', 'map', 'nan' ]

Four things happened at once:

  1. The Date came back as a string. Same instant, but typeof is now "string" and .getTime() is gone.
  2. The Map came back as {}. An empty object, not a Map, and not even its entries.
  3. The undefined property disappeared. Not copied as undefined, gone. Object.keys no longer lists it.
  4. NaN came back as null. JSON cannot express NaN, so it was written as null and read back as null.

Then the same object through structuredClone:

const viaClone = structuredClone(original);
console.log(viaClone.date instanceof Date);   // true
console.log(viaClone.map instanceof Map);     // true
console.log("undef" in viaClone);             // true
console.log(Number.isNaN(viaClone.nan));      // true

All four survived on my run.

Why the JSON trick fails

JSON.stringify only knows the JSON types: object, array, string, number, boolean, null. Anything outside that set gets converted or dropped, and the rules are not the ones you would pick yourself:

  • Date has a toJSON() method, so it becomes an ISO string.
  • Map and Set serialize as {}, because their entries live in internal slots, not own enumerable properties.
  • undefined and functions are skipped in objects, and become null inside arrays.
  • NaN and Infinity become null, because JSON numbers cannot express them.

So the trick is a serializer round trip, not a copy. What you get back is a JSON-shaped object, which is a different thing from the object you started with.

When the JSON trick is still fine

If the data came from an API and is already plain JSON (strings, numbers, booleans, null, arrays, plain objects), the round trip is safe and cheap. Config objects, request bodies, cached payloads: all fine. That is why the trick survives in so many codebases.

It stops being fine the moment the object holds something with a type: Dates from a database driver, Maps used as indexes, class instances, BigInt, typed arrays.

structuredClone has limits too

structuredClone implements the structured clone algorithm, and it handles Date, Map, Set, RegExp, typed arrays, ArrayBuffer and circular references. Its own limits:

  • Functions and DOM nodes throw a DataCloneError.
  • Class instances lose their prototype: you get a plain object with the same fields.
  • Property descriptors and getters are not preserved, values are read and copied.

If you need a class instance back with its behavior, write a clone() method on the class instead of reaching for a generic clone.

The rule I use now

  • Plain data that came from JSON: either one works. structuredClone if you want a single habit.
  • Anything with a Date, Map, Set or class instance: structuredClone.
  • A class with methods you need: its own clone().

Tested on Node.js 22. The narration in the video is an AI-generated voice; the output above is from a real run.

30-second version: https://www.youtube.com/shorts/52BkUbIV4oI

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.