Created
November 20, 2020 13:30
-
-
Save wperron/85cf816f302f23e6c3f5b308223bf094 to your computer and use it in GitHub Desktop.
palindrome check benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts"; | |
import { | |
bench, | |
runBenchmarks, | |
} from "https://deno.land/[email protected]/testing/bench.ts"; | |
function isPalindromeSimple(s) { | |
return s === s.split("").reverse().join(""); | |
} | |
function isPalindromeLoop(s) { | |
for (let i = 0; i < s.length / 2; i++) { | |
if (s[i] !== s[s.length - i - 1]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
Deno.test({ | |
name: "is palindrome - simple", | |
async fn() { | |
assert(isPalindromeSimple("laval")); | |
assert(isPalindromeSimple("abba")); | |
assert(!isPalindromeSimple("abcd")); | |
assert(!isPalindromeSimple("abcde")); | |
} | |
}); | |
Deno.test({ | |
name: "is palindrome - loop", | |
async fn() { | |
assert(isPalindromeLoop("laval")); | |
assert(isPalindromeLoop("abba")); | |
assert(!isPalindromeLoop("abcd")); | |
assert(!isPalindromeLoop("abcde")); | |
} | |
}); | |
bench({ | |
name: "is palindrome - simple", | |
runs: 1000, | |
func(b) { | |
b.start(); | |
for (let i = 0; i < 1e5; i++) { | |
isPalindromeSimple("lalalalalalalalalalalalalalalalalalalalalalalalalal"); | |
} | |
b.stop(); | |
}, | |
}); | |
bench({ | |
name: "is palindrome - loop", | |
runs: 1000, | |
func(b) { | |
b.start(); | |
for (let i = 0; i < 1e5; i++) { | |
isPalindromeLoop("lalalalalalalalalalalalalalalalalalalalalalalalalal"); | |
} | |
b.stop(); | |
}, | |
}); | |
runBenchmarks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment