JSON vs replacer

Benchmark created on


Setup

function replacer(key, value) {
  if (this[key] instanceof Date) return { $date: this[key].toISOString() };
  return value;
}


const reviver = (key, value) =>
  (value instanceof Object && !!value.$date && new Date(value.$date)) || value;

data = Array(100).fill(0).map(() => new Date(Math.round(Math.random()*1000000000000)));

Test runner

Ready to run.

Testing in
TestOps/sec
Pure
JSON.parse(JSON.stringify(data));
ready
With reviver
const json = JSON.stringify(data, replacer);
const result = JSON.parse(json, reviver);
ready

Revisions

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