Test name | Executions per second |
---|---|
Use hasClass shorthand | 2646564.2 Ops/sec |
Use classList | 3254439.5 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");