JavaScript Enumeration object - faster with strings, or numbers? -
in project working on, have script 'enumeration' object in it, such:
var myenumeration = { top: "top", right: "right", bottom: "bottom", left: "left" };
when have use use 1 of literal values, comparison myenumeration.left
or whatever case is.
however: @ least in c#, string values evaluate more numbers, since strings have character-for-character comparision ensure string matches string b.
this brings me question: implementing myenumeration
number values perform more quickly?
var myenumeration = { top: 0, right: 1, bottom: 2, left: 3 };
when comparing strings, javascript compares each characters 1 one, left-to-right. when comparing 2 numbers, it's single comparison. can guess 1 faster - numerical comparison.
however, kind of optimization might premature. unless need efficiency gain (comparing hundreds of values frequently, perhaps?), don't think them.
Comments
Post a Comment