JSON Stringify vs structuredVlone

Benchmark created on


Setup

const largeArray = Array.from({ length: 1e6 }).map(() => ({ key: "value" }));

const simpleObject = {"key1": "value"};
const simpleValues = {
  num: 1,
  bool: true,
  string: "string",
  null: null,
  undefined: undefined
};
const complexValues = {
  array: [1, 2, 3, 4, 5],
  object: { key: "value" },
  date: new Date(),
}
const simpleArray = [1, 2, 3, 4, 5];
// [[1,2,3,4,[[1,2,3,4,[1,2,3,4,5]],...],...]
const deepArray = Array.from({ length: 25 }).map(() => [1, 2, 3, 4, Array.from({ length: 25 }).map(() => [1, 2, 3, 4, Array.from({ length: 25 }).map(() => [1, 2, 3, 4, 5])])]);
// {key0: {num: 1, bool: true, string: "string", null: null, undefined: undefined, deep: {array: ...}}, key1: {...}, ...}
const deepObject = Array.from({ length: 25 }).reduce((acc, _, i) => {
  acc[`key${i}`] = { ...simpleObject, deep: { ...complexValues, deep: { ...simpleObject } } };
  return acc;
}, {});

Test runner

Ready to run.

Testing in
TestOps/sec
structuredClone simple object values
let clone = structuredClone(simpleObject);
ready
structuredClone complex object values
let clone = structuredClone(complexValues);
ready
structuredClone simple array
let clone = structuredClone(simpleArray);
ready
structuredClone deep array
let clone = structuredClone(deepArray);
ready
structuredClone large array
let clone = structuredClone(largeArray);
ready
structuredClone deep object
let clone = structuredClone(deepObject);
ready
JSON copy simple object values
const jsonCopy = JSON.parse(JSON.stringify(simpleObject));
ready
JSON copy complex object values
const jsonCopy = JSON.parse(JSON.stringify(complexValues));
ready
JSON copy simple array
const jsonCopy = JSON.parse(JSON.stringify(simpleArray));
ready
JSON copy deep array
const jsonCopy = JSON.parse(JSON.stringify(deepArray));
ready
JSON copy large array
const jsonCopy = JSON.parse(JSON.stringify(deepArray));
ready
JSON copy deep object
const jsonCopy = JSON.parse(JSON.stringify(deepObject));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.