Is Palindrome? (v5)

Revision 5 of this benchmark created by pixshatterer 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) {
    return str.substring(0, Math.floor(str.length / 2)) == (str.substring(Math.ceil(str.length / 2)).split('').reverse().join(''));
  }

function isPalindrome(str) {
        var halfLimit, mirrorStr,
                SPECIAL_CHARS_EXP = /[áéíóú\W]/gi;
                SPECIAL_CHARS_DICT = {
                        'á' : 'a',
                        'é' : 'e',
                        'í' : 'i',
                        'ó' : 'o',
                        'ú' : 'u'
                };
                
        str = str.toLowerCase().replace(SPECIAL_CHARS_EXP,function(matchedChar){
                return SPECIAL_CHARS_DICT[matchedChar]? SPECIAL_CHARS_DICT[matchedChar] : '';
        });

        halfLimit = str.length / 2 + (str.length % 2),
        mirrorStr = str.substr(halfLimit).match(/\w/g).reverse().join('');
        
        return str.indexOf(mirrorStr) === 0;
}
</script>

Setup

var str1 = "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba";
    var str2 = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba";
    var str3 = "abcdefghijklmnopqrstuvwxyzZyxwvutsrqponmlkjihgfedcba";
    var str4 = "Átale, demoníaco Caín, o me delata.";
    var str5 = "¿Acaso hubo búhos acá?";

Test runner

Ready to run.

Testing in
TestOps/sec
native strip reverse
f1(str1) === true;
f1(str2) === true;
f1(str3) === false;
f1(str4) === true;
f1(str5) === true;
ready
for-loop character comparison
f2(str1) === true;
f2(str2) === true;
f2(str3) === false;
f2(str4) === true;
f2(str5) === true;
ready
reverse half
f3(str1) === true;
f3(str2) === true;
f3(str3) === false;
f3(str4) === true;
f3(str5) === true;
ready
checking chars
isPalindrome(str1) === true;
isPalindrome(str2) === true;
isPalindrome(str3) === false;
isPalindrome(str4) === true;
isPalindrome(str5) === true;
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