Pure js hasClass vs jQuery hasclass (v28)

Revision 28 of this benchmark created on


Preparation HTML

<div class="super cali frag ilist ic expi ali docious tinker tailor soldier sailor richman poorman beggarman Thief" 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(el, selector) {
    return (el.className.indexOf(selector) >= 0);
  }

  function hasClassRegex(el, selector) {
    return (el.className && new RegExp("(^|\\s)" + selector + "(\\s|$)").test(el.className));
  }

  function classListContains(el, selector){
    return el.classList.contains(selector);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
$(el).hasClass('Thief');
ready
Pure JS - indexOf
hasClass(el, 'Thief');
ready
Pure JS - regex
hasClassRegex(el, 'Thief');
ready
DOM
classListContains(el, 'Thief');
ready

Revisions

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