Ad
  • Custom User Avatar

    I bet under the covers calling toUpperCase on a string loops through the string one character at a time and swaps to the uppercase char, which is pretty much the same as what's happening above; the complexity certainly isn't higher--O(n)--either way.

    You're right about the O(n) complexity, but you're still adding the overhead of an extra function call for every character instead of one function call for the whole string.

    I did a jsperf and it indicates performance is significantly improved just by moving .toUpperCase() out of the map function and putting it before .split('') so that it's outside of that loop... http://jsperf.com/touppercase-function-call

  • Custom User Avatar

    Since the solution grouping algorithm groups without respect to semicolons, it's not really fair to say "this" isn't best practice because the semicolons are omitted; "this" includes several solutions with complete semicolon usage (including my own).

    With regard to toUpperCase... yeah, I realized that afterward, though I'm not really sure about the savings. I bet under the covers calling toUpperCase on a string loops through the string one character at a time and swaps to the uppercase char, which is pretty much the same as what's happening above; the complexity certainly isn't higher--O(n)--either way.

  • Custom User Avatar

    It's minor but I think you'd get better performance calling toUpperCase once on the word rather than separately on every individual letter.

    Also, omitting semicolon line terminators isn't best practice. It's acceptable when you're trying to minify/obfuscate your code; otherwise they should be included. (I don't know which variation you saw but the one I see has 4 missing semicolons.)