Test name | Executions per second |
---|---|
Use hasClass shorthand | 1436421.9 Ops/sec |
Use classList | 1792645.9 Ops/sec |
Use classname match | 1132708.0 Ops/sec |
<div id="foo" class="bar baz"></div>
var test_element = document.getElementById("foo");
function hasClass(element, classes) {
classes = classes.split(' ');
for (var i = 0; i < classes.length; i++) {
if (!element.classList.contains(classes[i])) {
return false;
}
}
return true;
};
hasClass(test_element, "bar baz");
test_element.classList.contains("bar");
test_element.classList.contains("baz");
test_element.className.match('bar');
test_element.className.match('baz');