<div id="foo"></div>
var element = document.getElementById("foo");
var i = 100;
while (i--) {
element.setAttribute("class", "bar");
}
var element = document.getElementById("foo");
var i = 100;
while (i--) {
element.classList.add("bar");
}
var element = document.getElementById("foo");
var i = 100;
while (i--) {
element.classList.value = "bar";
}
var element = document.getElementById("foo");
var i = 100;
while (i--) {
element.className = "bar";
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
setAttribute(...) | |
classList.add(...) | |
classList.value | |
ClassName |
Test name | Executions per second |
---|---|
setAttribute(...) | 108444.5 Ops/sec |
classList.add(...) | 53317.8 Ops/sec |
classList.value | 160853.6 Ops/sec |
ClassName | 220848.3 Ops/sec |
Measuring the performance of different approaches to set class names on an HTML element is a useful benchmark.
Benchmark Overview
The provided JSON defines four test cases:
setAttribute(...)
: Sets the class name using the setAttribute
method.classList.add(...)
: Adds a class name to the element's class list using the classList.add
method.classList.value
: Sets the value of the class list property using the classList.value
syntax.className
: Sets the class name directly on the element's className
property.Options Comparison
The four approaches have different pros and cons:
setAttribute(...)
: This method is straightforward but may not be as efficient as other methods since it involves setting a DOM property, which can lead to additional overhead.classList.add(...)
: This method adds a class name to the element's class list and is generally considered more efficient than setting individual properties. It also provides a way to dynamically add classes without having to manually manage the class names in your code.classList
method), slightly less readable syntax than other methods.classList.value
: This approach sets the value of the class list property and is similar to using a string of class names separated by spaces. It's relatively efficient but might not be as readable as other methods since it uses a non-standard syntax.classList
method).className
: This approach sets the class name directly on the element's className
property and is relatively simple to implement. However, it may not be as efficient as other methods since it involves setting a DOM property.Library Considerations
Two libraries are used in the benchmark:
classList
: This is a part of the HTML5 specification and allows for dynamic class addition and removal. It's included in most modern browsers.Special JS Features or Syntax
The benchmark uses two special features:
classList
: This feature is part of the HTML5 specification and allows for dynamic class addition and removal. It's included in most modern browsers.Overall, the benchmark highlights the relative efficiency of different approaches to set class names on an HTML element. The classList.add
method is generally considered the most efficient approach due to its concise syntax and ability to dynamically add classes.
Alternative Approaches
Other alternative approaches to setting class names include:
addClassClass
method to add classes to an element.