Is Palindrome?

JavaScript performance comparison

Revision 2 of this test case created by Rhys Brett-Bowen


a silly test of palindrome testing techniques

Preparation code

function f1(str) {
   return str === str.split('').reverse().join('');

function f2(str) {
   for (var i=0; i<Math.floor(str.length / 2); i++) {
      if (str.charAt(i) !== str.charAt(str.length-i-1)) 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(''));

Benchmark.prototype.setup = function() {
    var str1 = "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba";
    var str2 = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba";
    var str3 = "abcdefghijklmnopqrstuvwxyzZyxwvutsrqponmlkjihgfedcba";

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;
for-loop character comparison
f2(str1) === true;
f2(str2) === true;
f2(str3) === false;
reverse half
f3(str1) === true;
f3(str2) === true;
f3(str3) === false;

Compare results of other browsers


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:


Add a comment