Anagrams
JavaScript performance comparison
Preparation code
<script>
function ana(str, str2) {
return str.split('').sort().join('') == str2.split('').sort().join('');
}
function ana2(str, str2) {
if (str.length != str2.length) return false;
var table = {};
for (var i = 0; i < str.length; i++) {
table[str[i]] = table[str[i]] ? table[str[i]] + 1 : 1;
table[str2[i]] = table[str2[i]] ? table[str2[i]] - 1 : -1;
}
for (var k in table)
if (table[k] != 0) return false;
return true;
}
function ana3(str, str2) {
var len = str.length;
if (len !== str2.length) return false;
var table = new Object();
for (var c1, c2, i=0; i<len; i++) {
c1 = str[i];
c2 = str2[i];
table[c1] = table[c1] ? table[c1] + 1 : 1;
table[c2] = table[c2] ? table[c2] - 1 : -1;
}
for (var k in table)
if (table[k] !== 0) return false;
return true;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
One-liner |
|
pending… |
Logical |
|
pending… |
Faster? |
|
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
- Revision 7: published
- Revision 8: published
0 comments