Hacker News new | ask | show | jobs
by caller9 5228 days ago
I cant speak for that guy but my take is that all of the strings except rarrr should statically defined in the object that defines their behaviour. They may even be integers instead of strings to speed things up. Then you would need some way to avoid collision for the event integers shared across many objects.

Repeating strings is hard to refactor and comparing strings is much less efficient than comparing integers. Think about making sure every bit in two 2 * 8 * length string of bits plus overhead is the same vs two 32bit integers. The latter far fewer cycles.

2 comments

I'm not convinced this is useful JavaScript optimization advice. JavaScript doesn't have integers, and string literals are interned, so it shouldn't make a substantial difference if you have the same literal written out 15 times or just defined once and placed in a property. (In fact, using the plain literal everywhere may be faster because it omits the dynamic property access.)
I'd rather mistype an identifier than a string literal, because nothing will complain about the wrong string literal.
Excellent point. I would normally do this, but I wanted to reduce the amount of indirection in the code as much as possible. But, it may still be worth doing so people aren't distracted by thinking "Wow, this girl should use constants".