Is Palindrome?

JavaScript performance comparison

Revision 6 of this test case created

Info

a silly test of palindrome testing techniques

Preparation code

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

function f2(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;
}
</script>
<script>
Benchmark.prototype.setup = function() {
    var str1 = "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba";
    var str2 = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba";
    var str3 = "abcdefghijklmnopqrstuvwxyzZyxwvutsrqponmlkjihgfedcba";
};
</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
native strip reverse
f1(str1) === true;
f1(str2) === true;
f1(str3) === false;
pending…
for-loop character comparison
f2(str1) === true;
f2(str2) === true;
f2(str3) === false;
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