I’m keeping track of things in an array currently, but I don’t need to know anything about them other than they’re in the array. For example, I might have a forSale array, and I have all the itemIDs that are for sale in the array.
Works fine for now, but anyways, I’ve been doing some testing and it seems as if object.hasOwnProperty(x) blows array.indexOf(x) !== -1 out of the water (I know they’re completely different but I don’t know of any other way to test if an array has a value in it). This got me to thinking, why not just create something like
var saleObject = {
"someId" : "",
"someOtherId" : ""
};
I can then call saleObject.hasOwnProperty('someId') and know that that object is on sale. I can see no downsides to this, but that’s why I’m here. Is there anything wrong in doing this?
No problem with it at all assuming you can modify the dependent code to accept the new structure. The difference in speed stems from the Object being able to do a Binary Search to determine whether it’s got the key, whereas the indexOf has to check every value in the array.