replace nested obj values with JSON.parse, JSON.stringify

Benchmark created on


Setup

const json = `{
  "uri": "composite-documents/a3d6a2ae-01c6-4cd5-8814-0983684be1dc",
  "guid": "1f4e034d-d994-45d4-be5d-7a325cd8926a"
}
`;

const meta = JSON.parse(json);

const transformInfinityWithJson = (obj) =>
  JSON.parse(JSON.stringify(obj), (_key, value) => {
    if (value === 'Infinity') {
      return Infinity;
    }
    return value;
  });

const transformInfinityWithObj = (obj) => {
  for (const key in obj) {
    if (typeof obj[key] === 'object') {
      transformInfinityWithObj(obj[key]);
    } else if (obj[key] === 'Infinity') {
      obj[key] = Infinity;
    }
  }
};

Test runner

Ready to run.

Testing in
TestOps/sec
as json
transformInfinityWithJson(meta);
ready
as object
transformInfinityWithObj(meta);
ready

Revisions

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