Map versus consts

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Map
const statesMap = new Map();

function state(state) {
  if (!statesMap.has(state)) {
    statesMap.set(state, `:state(${state})`);
  }
  return statesMap.get(state); // Moved inside the function
}

console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
console.log(`${state('foo')}`)
console.log(`${state('bar')}`)
console.log(`${state('baz')}`)
ready
Consts
const foo1 = ':state(foo)';
const bar1 = ':state(bar)';
const baz1 = ':state(baz)';
const foo2 = ':state(foo)';
const bar2 = ':state(bar)';
const baz2 = ':state(baz)';
const foo3 = ':state(foo)';
const bar3 = ':state(bar)';
const baz3 = ':state(baz)';
const foo4 = ':state(foo)';
const bar4 = ':state(bar)';
const baz4 = ':state(baz)';
const foo5 = ':state(foo)';
const bar5 = ':state(bar)';
const baz5 = ':state(baz)';
const foo6 = ':state(foo)';
const bar6 = ':state(bar)';
const baz6 = ':state(baz)';

console.log(`${foo1}`)
console.log(`${bar1}`)
console.log(`${baz1}`)
console.log(`${foo2}`)
console.log(`${bar2}`)
console.log(`${baz2}`)
console.log(`${foo3}`)
console.log(`${bar3}`)
console.log(`${baz3}`)
console.log(`${foo4}`)
console.log(`${bar4}`)
console.log(`${baz4}`)
console.log(`${foo5}`)
console.log(`${bar5}`)
console.log(`${baz5}`)
console.log(`${foo6}`)
console.log(`${bar6}`)
console.log(`${baz6}`)
ready

Revisions

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