Keys in JavaScript objects are strings. Javascript arrays (and strings) are Javascript objects. Therefore, keys of Javascript arrays are strings (they are string representations of numbers, to be precise, or of non-numbers if you explicitly add non-number keys to an array object).
Yes, it really is that absurd. And no, it doesn't necessarily impact performance, because modern Javascript engines are often smart enough to undo this oddity without changing the language semantics.
Posted Dec 21, 2012 15:00 UTC (Fri) by sorpigal (subscriber, #36106)
[Link]
it doesn't necessarily impact performance, because modern Javascript engines are often smart enough to undo this oddity without changing the language semantics
"Often" is not always; crazy isn't that far behind us.
I wonder, now, about the performance of code I've written where complex objects and functions have been used as keys. I imagined I was storing pointers, but if the key is really the stringification of the object... that can't be good.
I suppose it sort-of makes sense that somebody decided storing a numeric key as a string was less scary than storing it as a float, but the madness has to stop some time.