String Reverse Function Performance (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script>
var str = '',
    result;

// generate a random 20 character string 
(function () {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
      rnum;

  for (var i = 0; i < 20; i++) {
    rnum = Math.floor(Math.random() * 61);
    str += chars.substring(rnum, rnum + 1);
  }
}());

// decrementing for-loop with concatenation
function reverse_01 (s) {
  var o = '';

  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];

  return o;
}

// incrementing/decrementing for-loop with two arrays
function reverse_02 (s) {
  s = s.split('');
  var o = [];

  for (var i = s.length - 1, j = 0; i >= 0; i--, j++)
    o[j] = s[i];

  return o.join('');
}

// incrementing for-loop with array pushing and charAt
function reverse_03 (s) {
  var o = [];

  for (var i = 0, len = s.length; i <= len; i++)
    o.push(s.charAt(len - i));

  return o.join('');
}

// in-built functions
function reverse_04 (s) {
  return s.split('').reverse().join('');
}

// decrementing while-loop with concatenation and substring
function reverse_05 (s) {
  var i = s.length,
      o = '';

  while (i > 0) {
    o += s.substring(i - 1, i);
    i--;
  }

  return o;
}

// only for-loop declaration with concatenation 
function reverse_06 (s) {
  for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
  return o;
}

// recursion with substring and charAt
function reverse_07 (s) {
  return (s === '') ? '' : reverse_07(s.substr(1)) + s.charAt(0);
}

// internal function recursion
function reverse_08 (s) {
  function reverse (s, len, o) {
    return (len === 0) ? o : reverse(s, --len, (o += s[len]));
  };

  return reverse(s, s.length, '');
}

// half index switch for-loop
function reverse_09 (s) {
  s = s.split('');

  var len = s.length,
      half_index = Math.floor(len / 2) - 1,
      tmp;

  for (var i = 0; i <= half_index; i++) {
    tmp = s[len - i - 1];
    s[len - i - 1] = s[i];
    s[i] = tmp;
  }

  return s.join('');
}

// half-index recursion
function reverse_10 (s) {
  if (s.length < 2)
    return s;

  var half_index = Math.ceil(s.length / 2);
  return reverse_10(s.substr(half_index)) + reverse_10(s.substr(0, half_index));
}

function reverse_01_cached (s) {
  var o = '';
  
  slength= s.length;

  for (var i = slength - 1; i >= 0; i--)
    o += s[i];

  return o;
}

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
decrementing for-loop with concatenation
result = reverse_01(str);
ready
incrementing/decrementing for-loop with two arrays
result = reverse_02(str);
ready
incrementing for-loop with array pushing and charAt
result = reverse_03(str);
ready
in-built functions
result = reverse_04(str);
ready
decrementing while-loop with concatenation and substring
result = reverse_05(str);
ready
only for-loop declaration with concatenation
result = reverse_06(str);
ready
recursion with substring and charAt
result = reverse_07(str);
ready
internal function recursion
result = reverse_08(str);
ready
half index switch for-loop
result = reverse_09(str);
ready
half-index recursion
result = reverse_10(str);
ready
decrementing for-loop with concatenation [cached string length]
result = reverse_01_cached(str);
ready

Revisions

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