Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
Firefox 68
Windows
Desktop
5 years ago
Test name Executions per second
$.hasClass 1672749.0 Ops/sec
RegExp 7926486.5 Ops/sec
Dry RegExp 2084135.1 Ops/sec
indexOf 7400317.0 Ops/sec
classList 8853655.0 Ops/sec
hasClassString 4392369.5 Ops/sec
HTML Preparation code:
x
 
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
2
3
<div class="foobar" id="node">Hello</div>
Script Preparation code:
 
var node = document.getElementById('node');
var className = node.className;
var rx = new RegExp('\\b' + className + '\\b');
function hasClassString(e, c) {
  var s = e.className, i = s.indexOf(c);
  return i != -1 && (s.charCodeAt(i - 1) || 32) == 32 && (s.charCodeAt(i + c.length) || 32) == 32;
};
Tests:
  • $.hasClass

     
    var hasClass = $(node).hasClass(className);
  • RegExp

     
    var hasClass = rx.test(node.className);
  • Dry RegExp

     
    var drx = new RegExp('\\b' + className + '\\b');
    var hasClass = drx.test(node.className);
  • indexOf

     
    var hasClass = node.className.indexOf(className) !== -1;
  • classList

     
    var hasClass = node.classList.contains(className);
  • hasClassString

     
    var hasClass = hasClassString(node, className);