# Palindrome Test Performance

## Description

A test of the performance of various ways to solve the palindrome-test problem.

## Setup

``var str = 'Civic1';``

## Test runner

Testing in
TestOps/sec
Greg Foley v1
``````function isPalindrome (str) {
return str.toLowerCase().replace(/[\W_]/g, "") ===
str.toLowerCase().replace(/[\W_]/g, "").split("").reverse().join("");
}``````
Greg Foley v2
``````function isPalindrome (str) {
var sanitizedString = str.toLowerCase().replace(/[\W_]/g, "");
return sanitizedString === sanitizedString.split("").reverse().join("");
}``````
Aezed
``````function isPalindrome (str) {
return str.toLowerCase().match(/[a-z]/g).every(function (value, i, str) {
return value === str[str.length - i - 1];
});
}``````
Alexius
``````function isPalindrome (str) {

str = str.match(/[a-z]/gi).join('');
var emptyArray = [];

str.toLowerCase().split('').reduce(function(accumulator, value){
return emptyArray.unshift(value);
}, []);

return str.toLowerCase() === emptyArray.join('') ? true:false;
};``````
Andrew
``````function isPalindrome (str) {
return str.replace(/_ ]/g,"").toLowerCase() == str.replace(/_ ]/g,"").toLowerCase().split('').reverse().join('');
}``````
Daniel
``````function isPalindrome (str) {

var remove = str.toLowerCase().replace(/[^a-z0-9]/g,"");

var reverse = remove.split("").reverse().join("");

return reverse === remove;

};``````
Frances
``````function isPalindrome (str) {
str = str.toLowerCase().replace(/[^a-z]/gi, '');
var letters = str.split('');
for(var i = 0; i < letters.length/2; i++) {
if(letters[i] !== letters[letters.length-1-i]){
return false;
}
}
return true;
};``````
Jesse
``````function isPalindrome (str) {
// Write your code here, and

// change str to all lowercase and remove any whitespace to easily compare
str = str.toLowerCase().replace(/\s/g, '');

// reversed str: split str into array, reverse and join back into a string
strReversed = str.split('').reverse().join('');

// compare both strings
return strReversed === str;

}``````
joe
``````function isPalindrome (str) {
// Write your code here, and

str = str.toLowerCase();
str = str.replace(/[^a-z]/g,'');
var strA = str.slice(0,str.length / 2);
var strB = str.slice(Math.ceil(str.length / 2)).split('').reverse().join('');

console.log(strA  , strB);

return strA === strB;
}``````
Leo
``````function isPalindrome (str) {
str = str.toLowerCase().split("");
str = str.filter(function(space){
return /\S/.test(space)
});
var output = "";
for(var i = str.length-1; i>=0; i--){
output += str[i];
}
if(output === str.join("")){
return true;
}
return false;
}``````
Richard
``````function isPalindrome (str) {
str = str.toLowerCase().replace(/[^a-z]/gi, '');
if (str.length <= 1) {
return true;
}
if (str[0] !== str[str.length-1]) {
return false;
}
return isPalindrome(str.slice(1, str.length-1));
}``````
Sepehr
``````function isPalindrome (str) {
var str = str.replace(/\s/g,'').toLowerCase().split("");
var rev = [];
for (var i = 0; i < str.length; i++) {
rev[i] = str[str.length-1-i];
}
return str.join("") === rev.join("");
}``````
Steven v1
``````function isPalindrome (str) {
var stringy = str.replace(/[^a-z]/gi,'').toLowerCase();
return stringy.split('').reverse().join('') === stringy;
}``````
Steven v2
``````function isPalindrome(str) {
stringy = str.replace(/[^a-z]/gi,'').toLowerCase();
var innerFunc = function(strng) {
if (strng.length <= 1) {
return true;
}
if (strng[0] !== strng[strng.length - 1]) {
return false;
} else {
return isPalindrome(strng.slice(1, strng.length - 1));
}
};
return innerFunc(stringy);
}``````
Zac
``````function isPalindrome (str) {
var i = 0;
str = str.toLowerCase().split('').reduce(function(onlyLetters, char) {
if (char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122) {
return onlyLetters + char;
}
return onlyLetters;
}, '');

return str.split('').reverse().reduce(function(isPally, letter) {
if (letter !== str[i++]) return false;
return isPally;
}, true)
}``````