deep equal vs hash comparison

Benchmark created on


Preparation HTML

  <script type="module">
    import stringify from 'https://cdn.jsdelivr.net/npm/json-stable-stringify@1.1.1/+esm';
    window.stringify = stringify;
  </script>
  <script type="module">
    import { Md5 } from 'https://cdn.jsdelivr.net/npm/ts-md5@1.3.1/+esm';
    window.Md5 = Md5;
  </script>
  <script type="module">
    import { dequal } from 'https://cdn.jsdelivr.net/npm/dequal@2.0.3/lite/index.mjs';
    window.deepEqual = dequal;
  </script>

Setup

const obj1 = Array.from({ length: 100 }).reduce(
  o => ({ bbbbbbb: '11111111', cccccc: '111111111', aaaaaaa: o }),
  {}
);
const obj2 = Array.from({ length: 100 }).reduce(
  o => ({ bbbbbbb: '11111111', cccccc: '111111111', aaaaaaa: o }),
  {}
);

function createHash(obj) {
  function replacer(key, value) {
    if (value instanceof RegExp) return '__REGEXP ' + value.toString();
    else return value;
  }
  return Md5.hashStr(stringify(obj, { replacer }));
}

Test runner

Ready to run.

Testing in
TestOps/sec
deep Equal
deepEqual(obj1, obj2)
ready
compare hashes
createHash(obj1) === createHash(obj2)
ready

Revisions

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