2

What's the benefit of using a map over using this simple object to run a switch statement? It looks like the object method is more simple and probably faster to run. What is the use case for a map versus an object?

Map

var myMap = new Map()
     .set('string', 0) 
     .set('number', 1);


function convertSomething(mode){
     switch(mode){
        case myMap.get('string'):
             break;{
        case myMap.get('number'):
             break;
    }
}

Obj

var myObj = {
    string : 0,
    number : 1
 }

function convertSomething(mode){
     switch(mode){
        case myObj.string:
             break;{
        case myObj.number :
             break;
    }
}
Tester232323
  • 301
  • 1
  • 11
  • 2
    The basic question about map vs. object is useful, but in this case you are trying to essentially "fake" an enum. In JS, it is more idiomatic to simply use string-valued enum values. –  May 29 '16 at 18:27
  • Well, the answer this is claimed to be a duplicate of actually provides no useful information, including about performance, and mentions issues such as iteration order which is irrelevant here, and does not mention issues such as the syntactic infelicities in initializing maps...so it should not have been closed IMHO. –  May 29 '16 at 19:02
  • 1
    Related: http://stackoverflow.com/questions/18541940/map-vs-object-in-javascript –  May 29 '16 at 19:03
  • 1
    Keys are not converted to strings, try `a = new Map(); a.set(42, 'x'); a.get('42') // undefined` vs `a = {}; a[42] = 'x'; a['42'] // 'x'`\ – GingerPlusPlus May 29 '16 at 19:07

0 Answers0