Is Palindrome?
JavaScript performance comparison
Info
a silly test of palindrome testing techniques
Preparation code
<script>
function f1(str) {
return str === str.split('').reverse().join('');
}
function f2(str) {
var i = str.length - 1;
var k = 0;
while (i > k) {
if (str.charAt(k++) !== str.charAt(i--)) 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(''));
}
</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.
| Test | Ops/sec | |
|---|---|---|
native strip reverse |
|
pending… |
for-loop character comparison |
|
pending… |
reverse half |
|
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:
- Revision 1: published by Kyle Simpson
- Revision 2: published by Rhys Brett-Bowen
- Revision 3: published
0 comments