hasclass variations
JavaScript performance comparison
Preparation code
<script>
function hasclassre(haystack, needle) {
return RegExp("\\b" + needle + "\\b").test(haystack);
}
function hasclassre2(haystack, needle) {
return RegExp(' ' + needle + ' ').test(' ' + haystack + ' ');
}
var rclass = /[\t\r\n]/g;
function hasclassjq(haystack, needle) {
return (' ' + haystack + ' ').replace(rclass, ' ').indexOf(' ' + needle + ' ') >= 0;
}
var haystack = "html-ltr document hasJS";
console.log(hasclassre(haystack, "html-ltr"));
console.log(hasclassre(haystack, "hasJS"));
console.log(hasclassre(haystack, "unknown"));
console.log(hasclassre2(haystack, "html-ltr"));
console.log(hasclassre2(haystack, "hasJS"));
console.log(hasclassre2(haystack, "unknown"));
console.log(hasclassjq(haystack, "html-ltr"));
console.log(hasclassjq(haystack, "hasJS"));
console.log(hasclassjq(haystack, "unknown"));
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
re |
|
pending… |
re2 |
|
pending… |
jq |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments