i like using Object lookup tables, because it can saves quotes but always executes much faster than searching via indexOf:
var oks= {"www.google.com":1, "www.jsfiddle.net":1}
if(oks[location.host]===1){
alert("ok!")
}
it's much faster because there's no method call and property lookup is very fast. it's even faster than switch on large collections, and certainly more flexible and re-usable.
anytime i need one value from another value, without nested conditions or other complications, i go for the LUT.
this pattern also allows you to easily add new values (oks[key]=1
), use extend() to merge collections without dupes, and semantically remove un-needed choices (delete oks['www.google.com']
), instead of a bunch of splice(indexOf(),1) malarky. it also works great in old browsers, unlike [].indexOf()...
you can use hasOwnProperty.call(oks, key)
as well, but it negates some performance benefits.
also, this is really only good for functions, strings, numbers, and some arrays, it won't allow lookup by objects, like a Map() would.
i should mention there's also another IE8-friendly way of doing indexOf:
if(",www.google.com,www.jsfiddle.net,".indexOf([,key,])!==1){...}