String Reverse Function Performance

JavaScript performance comparison

Test case created

Preparation code

<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));
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
decrementing for-loop with concatenation
result = reverse_01(str);
pending…
incrementing/decrementing for-loop with two arrays
result = reverse_02(str);
pending…
incrementing for-loop with array pushing and charAt
result = reverse_03(str);
pending…
in-built functions
result = reverse_04(str);
pending…
decrementing while-loop with concatenation and substring
result = reverse_05(str);
pending…
only for-loop declaration with concatenation
result = reverse_06(str);
pending…
recursion with substring and charAt
result = reverse_07(str);
pending…
internal function recursion
result = reverse_08(str);
pending…
half index switch for-loop
result = reverse_09(str);
pending…
half-index recursion
result = reverse_10(str);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment