Considering creating a JavaScript enum library involves deciding how to store the values, I'm at a crossroads. Should I opt for speed in comparison or prioritize readability during debugging by using strings or numbers? While objects are an option too, that leads to another question.
For instance:
// Avoiding this due to lack of readability when debugging
var Planets = {Earth:0, Mars:1, Venus: 2}
// Preferring this for clarity with Planets.Earth returning a readable value ("Earth")
var Planets = {Earth: 'Earth', Mars: 'Mars'}
Concerns arise when comparing them with if (myPlanet === Planet.Earth)
. Could string comparison potentially slow down performance, especially in loops? The ECMAScript specification suggests so.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
In testing, both string and number comparisons seem to take equal time, as shown here: http://jsperf.com/string-comparison-versus-number-comparison/2. It doesn't appear to scan the entire string.
While this may be a minute optimization, my query remains: does string equality comparison utilize pointers, making it as efficient as number equality comparison?