map-vs-object-when-keys-are-numbers

Benchmark created by T.J. Crowder on


Setup

const obj = Object.create(null);
  const map = new Map();
  const checks = [];
  for (let n = 0; n < 1000; ++n) {
    const entry = {value: n};
    obj[n] = entry;
    map.set(n, entry);
    if (Math.random() < 0.5) {
        checks.push(n);
    }
  }

Test runner

Ready to run.

Testing in
TestOps/sec
map
checks.forEach(n => {
    if (map.get(n).value !== n) {
        throw new Error("Error in test");
    }
});
ready
obj
checks.forEach(n => {
    if (obj[n].value !== n) {
        throw new Error("Error in test");
    }
});
ready

Revisions

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

  • Revision 1: published by T.J. Crowder on