Skip to curriculum
Jump to the editor

22.7. Compare structuredClone with JSON copying

Easy
Topics

This question is in

22. Serialization
10–20 min

For years the shortest way to deep-copy a value was JSON.parse(JSON.stringify(value)). Browsers now have structuredClone(value), the same algorithm that copies data into a Web Worker or through postMessage. The two look interchangeable on plain objects, and differ on almost everything else: Dates, undefined, Maps, BigInts, cycles, shared objects and functions. Knowing the differences tells you which one a piece of state can survive.

Predict what each snippet below gives, and write the answers in answers.js.

Files

  • answers.js: an object answers with one entry per snippet, 1 to 16. Write the value the snippet evaluates to ("string", 3, true, null…). When the snippet throws, write "throws " followed by the error's name, for example "throws RangeError".

The snippets use this helper and this class:

JAVASCRIPT
const jsonCopy = (value) => JSON.parse(JSON.stringify(value));
class Point { constructor(x) { this.x = x; } }

Requirements

Each snippet is one requirement, checked by one test:

JAVASCRIPT
// 1
typeof jsonCopy({ when: new Date(0) }).when
// 2
structuredClone({ when: new Date(0) }).when instanceof Date
// 3
Object.keys(jsonCopy({ a: undefined, b: 1 })).length
// 4
"a" in structuredClone({ a: undefined, b: 1 })
JAVASCRIPT
// 5
jsonCopy({ n: NaN }).n
// 6
JSON.stringify(jsonCopy(new Map([["k", 1]])))
// 7
structuredClone(new Map([["k", 1]])).get("k")
// 8
jsonCopy({ big: 1n })
// 9
typeof structuredClone({ big: 1n }).big
JAVASCRIPT
// 10
const a = {}; a.self = a; jsonCopy(a);
// 11
const a = {}; a.self = a; const copy = structuredClone(a); copy.self === copy;
// 12
const x = {}; const copy = jsonCopy({ p: x, q: x }); copy.p === copy.q;
// 13
const x = {}; const copy = structuredClone({ p: x, q: x }); copy.p === copy.q;
JAVASCRIPT
// 14
structuredClone({ f() {} })
// 15
structuredClone(new Point(1)) instanceof Point
// 16
structuredClone(/ab+c/gi).flags

(Each numbered snippet runs on its own, so the repeated names do not clash.)

Examples

JAVASCRIPT
// The shape of an answer, for snippets that are not in the list:
typeof jsonCopy({ n: 1 }).n        // "number"
jsonCopy([undefined]).length       // 1
structuredClone(Symbol("s"))       // "throws DataCloneError"

Environment

  • The tests run each snippet in the same page and compare your answer with what it gives, with Object.is.
  • Running the snippets yourself is allowed, but the point is to predict first and then check.

Hints

  1. JSON can only hold objects, arrays, strings, finite numbers, booleans and null. What happens to anything else, on the way out and on the way back?
  2. Which of the two copies remembers the objects it has already copied?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…