I'm creating a data structure that will contain that I will be using to repeatedly check to see if certain values are defined. I've come up with two possible solutions, and am wondering which is more efficient, or if there is a better way than either:
1) Use an array: keys = ['key1' , 'key2', 'key3']
I can create an array like this and then use jQuery.inArray(keyToCheck, keys) > -1
to check and see if keyToCheck
is in my array.
2) Use an object: keys = {key1 : 1, key2 : 1, key3: 1}
I can create this object and then use keys[keyToCheck] || 0
to see if keyToCheck
is defined.
What I'm not sure about is how searching an object is implemented in javascript, and whether or not it's more efficient than jQuery.inArray
, which loops through an array. Is there a performance difference between these methods? Using jQuery isn't an issue for me since I already have it in my code for other reasons.