mathiasbynens / esrever

A Unicode-aware string reverser written in JavaScript.
https://git.io/esrever
MIT License
891 stars 30 forks source link

Question about Step 2 #6

Closed mknecht closed 9 years ago

mknecht commented 10 years ago

more a question than an issue: Why do you manually reverse the string in step 2? After step 1, the otherwise incorrect string.split('').reverse().join('') idiom should work, right? (Tried it and the tests work.)

Is it for performance reasons? Because I would have thought copying a string over and over again yields O(n²), where the array approach would yield O(n).

Other than that thanks for the insightful article on esrever. :)

mathiasbynens commented 9 years ago

I haven’t done any performance testing on this, but turning the string into an array of characters only to turn it back into a string sounds like it would be more work than the current approach. That’s why I went for it.

mattgrande commented 8 years ago

I was curious, so I checked it out. You're right, @mathiasbynens. Your version is (much) faster.

http://jsperf.com/esrever-reverser

mathiasbynens commented 8 years ago

Thanks for following up on this! Your comment made me remember one of the very first jsPerf tests ever created: https://jsperf.com/string-reverse