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 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="foobar" id="node">Hello</div>
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;
};
var hasClass = $(node).hasClass(className);
var hasClass = rx.test(node.className);
var drx = new RegExp('\\b' + className + '\\b');
var hasClass = drx.test(node.className);
var hasClass = node.className.indexOf(className) !== -1;
var hasClass = node.classList.contains(className);
var hasClass = hasClassString(node, className);