map vs object for small enum

Benchmark created by gartz on


Setup

const map = new Map([
    [1, 1],
    [2, 1],
    [3, 1],
    [4, 1],
    [5, 1],
    [6, 1],
  ]);
  
  const obj = {
    [1]: 1,
    [2]: 1,
    [3]: 1,
    [4]: 1,
    [5]: 1,
    [6]: 1,
  };

Test runner

Ready to run.

Testing in
TestOps/sec
map
const a = map.has(4) ? map.get(4) : -1;
const b = map.has(10) ? map.get(10) : -1;
ready
obj
const a = obj[4] || -1;
const b = obj[10] || -1;
ready

Revisions

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