Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className vs. classList v2
(version: 0)
Comparing performance of:
className vs classList
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo" class="test"></div>
Tests:
className
var element = document.getElementById("foo"); var i = 1000; while (i--) { if (element.className.indexOf("test") > -1) { element.className = "bar"; } else { element.className = "test"; } }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { if (element.classList.contains("test")) { element.classList.add("bar"); } else { element.classList.add("test"); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
className
classList
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance of two different approaches to update an HTML element's class names: 1. `className`: This approach uses the `indexOf` method to check if the element's current class name contains the string "test". If it does, it replaces the class name with "bar". 2. `classList`: This approach uses the `contains` and `add` methods of the `classList` property to achieve the same result. **Script Preparation Code:** There is no script preparation code provided, which means that the benchmark starts from a clean slate without any pre-existing scripts or variables. **Html Preparation Code:** The HTML preparation code creates an HTML element with the id "foo" and an initial class name of "test". **Individual Test Cases:** There are two test cases: 1. `className`: ```javascript var element = document.getElementById("foo"); var i = 1000; while (i--) { if (element.className.indexOf("test") > -1) { element.className = "bar"; } else { element.className = "test"; } } ``` This test case uses the `indexOf` method to check if the element's class name contains the string "test". 2. `classList`: ```javascript var element = document.getElementById("foo"); var i = 1000; while (i--) { if (element.classList.contains("test")) { element.classList.add("bar"); } else { element.classList.add("test"); } } ``` This test case uses the `contains` and `add` methods of the `classList` property to achieve the same result. **Library:** In both test cases, the `document.getElementById` method is used to retrieve the HTML element with the id "foo". This method is a part of the DOM (Document Object Model) API, which is built into most modern browsers. **Special JS Features/Syntax:** There are no special JavaScript features or syntax mentioned in the benchmark definition. Both test cases use standard JavaScript methods and properties. **Pros and Cons:** Here are some pros and cons of each approach: 1. `className`: * Pros: + Wide support across browsers + Simple and easy to understand * Cons: + Slow performance due to the use of the `indexOf` method, which has to iterate over the entire class name string 2. `classList`: * Pros: + Faster performance compared to the `className` approach + More efficient use of resources * Cons: + Requires modern browsers that support the `classList` property + May require additional setup or polyfills for older browsers **Other Alternatives:** If you need alternative approaches, here are a few options: 1. Using a library like jQuery, which provides an efficient way to update class names using its `.toggleClass()` method. 2. Implementing a custom solution that uses a more efficient algorithm, such as using a regular expression or a custom string manipulation function. However, the `classList` approach is generally considered the best choice for modern browsers, and the `className` approach can be used as a fallback for older browsers that don't support `classList`.
Related benchmarks:
className vs classList by Tyler
className.indexOf vs. classList.contains 1
jquery vs queryselectorAll vs getElementsByTagName 2
classList.contains vs. a
querySelector vs classList.contains
Comments
Confirm delete:
Do you really want to delete benchmark?