Reverse a String - Optimization

Tell us what’s happening:

For this challenge I figured out two alternate solutions. I was wondering if one was optimal over the other. As in, is there a resource for how much processing power a given function is likely to use? I doubt optimization is based purely on # of characters.

Your code so far

//Overcomplicating it for laughs.

function reverseString(str) {
  str = str.split("");
  var rts ="";
  for (var j = str.length; j>0; j--) {
   rts += str.pop();
  }
    
       return rts;
}

reverseString("hello");


//What I think was meant to be done.
/*function reverseString(str) {
  str = str.split("").reverse().join("");
    return str;
}  
  reverseString("hello");
  */

Link to the challenge:
https://www.freecodecamp.org/challenges/reverse-a-string

I made a jsperf: https://jsperf.com/reverse-a-string-yay

The longer one is slightly slower, but there isn’t really much of a noticeable difference in speed.

It’s the other way around. The longer one has slightly more ops/second, so it’s technically the faster one.

1 Like

Oh wow, that site’s even better than I was hoping for!

(Heard of it before actually, but had no clue what it was.)