Is Palindrome? (v7)

Revision 7 of this benchmark created by KC on


Description

a silly test of palindrome testing techniques

Preparation HTML

<script>
function f1(str) {
  return str === str.split('').reverse().join('');
}

function f2(str) {
  var i = str.length - 1;
  var k = 0;
  while (i > k) {
    if (str.charAt(k++) !== str.charAt(i--)) return false;
  }
  return true;
}

function f3(str) {
  var middle = Math.floor(str.length / 2),
  l = str.length;
  for (var i=0; i<middle; i++) {
    if (str.charAt(i) !== str.charAt(l-i-1)) return false;
  }
  return true;
}

function f4(str) {
  for (var i = 0; i < str.length/2 << 0; i++) {
    if (str.charAt(i) !== str.charAt(str.length-i-1)) {
      return false;
    }
  }
  return true;
}
</script>

Setup

var str1 = "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba";
    var str2 = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba";
    var str3 = "abcdefghijklmnopqrstuvwxyzZyxwvutsrqponmlkjihgfedcba";

Test runner

Ready to run.

Testing in
TestOps/sec
Native strip reverse
f1(str1) === true;
f1(str2) === true;
f1(str3) === false;
ready
While loop character comparison
f2(str1) === true;
f2(str2) === true;
f2(str3) === false;
ready
For loop
f3(str1) === true;
f3(str2) === true;
f3(str3) === false;
ready
For loop improved
f4(str1) === true;
f4(str2) === true;
f4(str3) === false;
ready

Revisions

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

  • Revision 1: published by Kyle Simpson on
  • Revision 2: published by Rhys Brett-Bowen on
  • Revision 3: published on
  • Revision 5: published by pixshatterer on
  • Revision 6: published on
  • Revision 7: published by KC on