Pure js hasClass vs jQuery hasclass (v35)

Revision 35 of this benchmark created by tomin on


Description

Additional comparison of className and getAttribute("class")

Preparation HTML

<div class="someClass" id="someElement"></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var el = document.getElementById('someElement');
  
function hasClass(elem, className) {
    return new RegExp(' ' + className + ' ').test(' ' + elem.getAttribute("class") + ' ');
}

function hasClass2(elem, className) {
        return ((" " + elem.getAttribute("class") + " ").replace(/[\n\t\r]/g, " ").indexOf(" " + className + " ") > -1);
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
$(el).hasClass('someClass');
ready
regex
hasClass(el, 'someClass');
ready
nonregex
hasClass2(el, 'someClass');
ready

Revisions

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