hasclass variations

JavaScript performance comparison

Test case created by satchmorun

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.

Testing in unknown unknown
Test Ops/sec
re
hasclassre(haystack, "html-ltr");
hasclassre(haystack, "hasJS");
hasclassre(haystack, "unknown");
pending…
re2
hasclassre2(haystack, "html-ltr");
hasclassre2(haystack, "hasJS");
hasclassre2(haystack, "unknown");
pending…
jq
hasclassjq(haystack, "html-ltr");
hasclassjq(haystack, "hasJS");
hasclassjq(haystack, "unknown");
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment