Reverse string

Benchmark created by Mathias Bynens on


Preparation HTML

<script>
  var str = 'abcdefghijklmnopqrstuvwxyz';
  
  function str_reverse1(str) {
   return str.split('').reverse().join('');
  }
  
  function str_reverse2(str) {
   var result = '',
       length = str.length;
   while (length--) {
    result += str[length];
   }
   return result;
  }
  
  function str_reverse3(str) {
   var result = [],
       length = str.length,
       pad = length;
   while (length--) {
    result[pad - length] = str[length];
   }
   return result.join('');
  }
  
  function str_reverse4(str) {
   var curr, result = str.split('');
   for (var i = 0, j = result.length - 1; i < j; i++, j--) {
    curr = result[i];
    result[i] = result[j];
    result[j] = curr;
   }
   return result.join('');
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
str.split('').reverse().join('')
str_reverse1(str);
ready
str_reverse2(str)
str_reverse2(str);
ready
str_reverse3(str)
str_reverse3(str);
ready
str_reverse4(str)
str_reverse4(str);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.