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