Accessing array elements faster with lookup objects

If you identify objects in an array by a unique property, turn the array into a lookup object. The unique property becomes a key, giving you access to the objects without having to search for them in the array first.

// this array holds objects that all have name- and type-properties
const array = [
  { name: 'Bulbasaur',  type: 'Grass/Poison' },
  { name: 'Charmander', type: 'Fire'         },
  { name: 'Squirtle',   type: 'Water'        },
  { name: 'Pikachu',    type: 'Electric'     }

// to get the type for a name, we need to .find() the element first
array.find(pokemon => === 'Charmander').type
// ⇒ 'Fire'

// this lookup object uses the unique names as keys
const object = {
  'Bulbasaur':  { name: 'Bulbasaur',  type: 'Grass/Poison' },
  'Charmander': { name: 'Charmander', type: 'Fire'         },
  'Squirtle':   { name: 'Squirtle',   type: 'Water'        },
  'Pikachu':    { name: 'Pikachu',    type: 'Electric'     }

// lookups work with bracket notation, which is faster than .find()
// ⇒ 'Fire'

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.