DEV Community

Discussion on: JS Performance: Perhaps We Shouldn't Always Use Arrays

Collapse
weswedding profile image
Weston Wedding

I love maps! I have often found myself getting pushed to use arrays despite my preferences by frameworks/libraries I am using or the simple convenience of being able to use .forEach(), .map(), etc.

At least In ES6-land we also finally have an actual Map object which gives us the ability to iterate over entries, easy size determination, using things other than strings as keys... probably other goodies.

const catsMap = new Map([
  ['Aeris', { name: 'Aeris', isFavourite: true }],
  ['Juri', { name: 'Juri' }],
  ['Dante', { name: 'Dante' }],
  ['Frankenstein', { name: 'Frankenstein' }],
  ['Aeris', { name: 'Aeris' }],
])

const findMyCat =
    requestedCat => catsMap.get(requestedCat)

console.log(findMyCat('Dante'))
console.log(findMyCat('Marmalade'))
Collapse
nimmo profile image
Nimmo Author

Ooh, I must have missed Map in JS, that's much better! When was this introduced? :-O

(I'll be honest, I posted this thinking "someone will tell me a better way to do this" - I'm glad I was proven correct! :-D )

Thanks <3

Collapse
weswedding profile image
Weston Wedding

Not sure, I actually stumbled on it by accident a while ago but haven't used it much because I never remember it exists.