map creation

Benchmark created on


Description

Tests various methods of creating a Map

Setup

const entries = Array.from({length: 10000}, ((_, idx) => ({
  k1: `key1-${idx}`,
  k2: `key2-${idx}`,
  k3: Math.random() > 0.5 ? `key3-${idx}` : null,
  k4: `key4-${idx}`,
})))

function createKey({k1, k2, k3,k4}) {
  return `${k1} | ${k2} | ${k3 ?? null} | ${k4}`
}

Test runner

Ready to run.

Testing in
TestOps/sec
individual set for...of
function createMapForOf(entries) {
  const lookupMap = new Map()

  for (const entry of entries) {
    const stringKey = createKey({k1: entry.k1, k2: entry.k2, k3: entry.k3, k4: entry.k4})

    lookupMap.set(stringKey, entry)
  }

  return lookupMap
}
createMapForOf(entries)
ready
Map constructor
function createMapConstructor(entries) {
  return new Map(entries.map(entry => [createKey(entry), entry]))
}
createMapConstructor(entries)
ready

Revisions

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