Skip to content

Instantly share code, notes, and snippets.

@fearmear
Created August 6, 2017 09:35
Show Gist options
  • Save fearmear/793742468cd1e7871ce513c1d408d53d to your computer and use it in GitHub Desktop.
Save fearmear/793742468cd1e7871ce513c1d408d53d to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/vewafe
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function isPalindromeReverse(word){
word = word.toLowerCase();
const part1 = word.slice(0, Math.floor(word.length / 2));
const part2 = word.slice(Math.ceil(word.length / 2));
return part1 === part2.split('').reverse().join('');
}
console.log(isPalindromeReverse('abba'));
console.log(isPalindromeReverse('abbba'));
console.log(isPalindromeReverse('madam'));
console.log(isPalindromeReverse('Civic'));
console.log(isPalindromeReverse('foobar'));
function isPalindromeLoop(word){
word = word.toLowerCase();
let index = 0;
while (index < word.length / 2) {
if (word[index] !== word[word.length - index - 1]) {
return false;
}
index++;
}
return true;
}
console.log(isPalindromeLoop('abba'));
console.log(isPalindromeLoop('abbba'));
console.log(isPalindromeLoop('madam'));
console.log(isPalindromeLoop('Civic'));
console.log(isPalindromeLoop('foobar'));
function profileFunction(fn, arg, logText){
const startTime = performance.now();
let i = 1000;
while (i) {
fn.call(null, arg);
i--;
}
console.log(fn.name, arg, performance.now() - startTime, logText);
}
const testValues = {
shortPalindrome: 'abba',
longPalindrome: 'abbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba',
shortNotPalindrome: 'foob',
longNotPalindrome: 'foobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoob'
};
profileFunction(isPalindromeReverse, testValues.shortPalindrome, 'faster, not sure why');
profileFunction(isPalindromeLoop, testValues.shortPalindrome, 'slower');
profileFunction(isPalindromeReverse, testValues.longPalindrome, 'faster');
profileFunction(isPalindromeLoop, testValues.longPalindrome, 'a lot slower because maximum amount of iterations is made');
profileFunction(isPalindromeReverse, testValues.shortNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.shortNotPalindrome, 'faster');
profileFunction(isPalindromeReverse, testValues.longNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.longNotPalindrome, 'a lot faster because 1 iteration is made');
</script>
<script id="jsbin-source-javascript" type="text/javascript">function isPalindromeReverse(word){
word = word.toLowerCase();
const part1 = word.slice(0, Math.floor(word.length / 2));
const part2 = word.slice(Math.ceil(word.length / 2));
return part1 === part2.split('').reverse().join('');
}
console.log(isPalindromeReverse('abba'));
console.log(isPalindromeReverse('abbba'));
console.log(isPalindromeReverse('madam'));
console.log(isPalindromeReverse('Civic'));
console.log(isPalindromeReverse('foobar'));
function isPalindromeLoop(word){
word = word.toLowerCase();
let index = 0;
while (index < word.length / 2) {
if (word[index] !== word[word.length - index - 1]) {
return false;
}
index++;
}
return true;
}
console.log(isPalindromeLoop('abba'));
console.log(isPalindromeLoop('abbba'));
console.log(isPalindromeLoop('madam'));
console.log(isPalindromeLoop('Civic'));
console.log(isPalindromeLoop('foobar'));
function profileFunction(fn, arg, logText){
const startTime = performance.now();
let i = 1000;
while (i) {
fn.call(null, arg);
i--;
}
console.log(fn.name, arg, performance.now() - startTime, logText);
}
const testValues = {
shortPalindrome: 'abba',
longPalindrome: 'abbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba',
shortNotPalindrome: 'foob',
longNotPalindrome: 'foobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoob'
};
profileFunction(isPalindromeReverse, testValues.shortPalindrome, 'faster, not sure why');
profileFunction(isPalindromeLoop, testValues.shortPalindrome, 'slower');
profileFunction(isPalindromeReverse, testValues.longPalindrome, 'faster');
profileFunction(isPalindromeLoop, testValues.longPalindrome, 'a lot slower because maximum amount of iterations is made');
profileFunction(isPalindromeReverse, testValues.shortNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.shortNotPalindrome, 'faster');
profileFunction(isPalindromeReverse, testValues.longNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.longNotPalindrome, 'a lot faster because 1 iteration is made');
</script></body>
</html>
function isPalindromeReverse(word){
word = word.toLowerCase();
const part1 = word.slice(0, Math.floor(word.length / 2));
const part2 = word.slice(Math.ceil(word.length / 2));
return part1 === part2.split('').reverse().join('');
}
console.log(isPalindromeReverse('abba'));
console.log(isPalindromeReverse('abbba'));
console.log(isPalindromeReverse('madam'));
console.log(isPalindromeReverse('Civic'));
console.log(isPalindromeReverse('foobar'));
function isPalindromeLoop(word){
word = word.toLowerCase();
let index = 0;
while (index < word.length / 2) {
if (word[index] !== word[word.length - index - 1]) {
return false;
}
index++;
}
return true;
}
console.log(isPalindromeLoop('abba'));
console.log(isPalindromeLoop('abbba'));
console.log(isPalindromeLoop('madam'));
console.log(isPalindromeLoop('Civic'));
console.log(isPalindromeLoop('foobar'));
function profileFunction(fn, arg, logText){
const startTime = performance.now();
let i = 1000;
while (i) {
fn.call(null, arg);
i--;
}
console.log(fn.name, arg, performance.now() - startTime, logText);
}
const testValues = {
shortPalindrome: 'abba',
longPalindrome: 'abbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba',
shortNotPalindrome: 'foob',
longNotPalindrome: 'foobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoobfoob'
};
profileFunction(isPalindromeReverse, testValues.shortPalindrome, 'faster, not sure why');
profileFunction(isPalindromeLoop, testValues.shortPalindrome, 'slower');
profileFunction(isPalindromeReverse, testValues.longPalindrome, 'faster');
profileFunction(isPalindromeLoop, testValues.longPalindrome, 'a lot slower because maximum amount of iterations is made');
profileFunction(isPalindromeReverse, testValues.shortNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.shortNotPalindrome, 'faster');
profileFunction(isPalindromeReverse, testValues.longNotPalindrome, 'slower');
profileFunction(isPalindromeLoop, testValues.longNotPalindrome, 'a lot faster because 1 iteration is made');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment