<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');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Use hasClass shorthand | |
Use classList | |
Use classname match |
Test name | Executions per second |
---|---|
Use hasClass shorthand | 10877790.0 Ops/sec |
Use classList | 14553115.0 Ops/sec |
Use classname match | 3496750.8 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark that compares three approaches for checking if an HTML element has a specific class: classList.contains
, loop
, and regular expression (regex
).
Approach 1: Using classList.contains
This approach uses the built-in classList.contains()
method of the Element
interface to check if the element has a specific class. The classList
property returns a DOMTokenList
object, which contains the classes that are applied to the element.
Pros:
Cons:
classList
.Approach 2: Using a Loop
This approach uses a simple loop to check if the element has each of the specified classes. It splits the class list into an array and then iterates over it, checking if the element's class list contains each class.
Pros:
classList
.Cons:
Approach 3: Using Regular Expressions
This approach uses regular expressions to match the specified classes against the element's class list. The match()
method is used to search for a pattern in the string (in this case, the class name).
Pros:
Cons:
Library Usage
In this benchmark, no libraries are explicitly used. However, it's worth noting that classList
is a property of the Element
interface, which is part of the DOM API (Document Object Model). This means that if you're using a library that modifies or extends the DOM, it may affect the performance of this benchmark.
Special JavaScript Features/Syntax
None are explicitly used in this benchmark. However, it's worth noting that some browsers may support additional features like let
and const
declarations, which can affect the performance of certain code blocks. The benchmark doesn't specifically test for these features, but they could potentially impact the results.
Alternatives
If you're looking for alternative approaches to this benchmark, here are a few options:
hasAttribute()
: Instead of checking if an element has a specific class, you can use hasAttribute()
with the class
attribute name.indexOf()
and includes()
: You can use indexOf()
to find the index of a class in the element's class list, and then use includes()
to check if the class is present.Array.prototype.some()
: This approach uses the some()
method to iterate over the class list and check if any of the classes match the specified class.These alternative approaches may have different performance characteristics or trade-offs in terms of readability, maintainability, and compatibility with older browsers.