Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Remove class 1111
(version: 0)
Comparing performance of:
className = "" vs removeAttribute("class") vs classList.remove("bar")
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
className = ""
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className = "bar"; element.className = ""; }
removeAttribute("class")
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className = "bar"; element.removeAttribute("class"); }
classList.remove("bar")
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className = "bar"; element.classList.remove("bar"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
className = ""
removeAttribute("class")
classList.remove("bar")
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
className = ""
4718.2 Ops/sec
removeAttribute("class")
4414.8 Ops/sec
classList.remove("bar")
2364.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark is designed to measure the performance of removing a class from an HTML element in different ways. The test cases are identical, except for the approach used to remove the class: 1. `className = ""` (using the assignment operator) 2. `removeAttribute("class")` 3. `classList.remove("bar")` **What's being tested** The benchmark is testing the performance of each approach in removing a class from an HTML element. The goal is to determine which method is the fastest. **Options compared** The three options are: 1. **`className = ""`**: This approach uses the assignment operator (`=`) to set the `className` property of the element to an empty string, effectively removing it. 2. **`removeAttribute("class")`**: This approach uses a built-in method (`removeAttribute`) to remove the specified attribute ("class") from the element. 3. **`classList.remove("bar")`**: This approach uses the `classList` property and its `remove` method to remove the specified class ("bar") from the element. **Pros and Cons of each approach** 1. **`className = ""`**: * Pros: Simple, lightweight, and widely supported. * Cons: Can be slower due to string concatenation or assignment, depending on the browser. 2. **`removeAttribute("class")`**: * Pros: Fast, as it's a built-in method. * Cons: May not work in older browsers that don't support this method, and can lead to unexpected behavior if used incorrectly (e.g., removing other attributes). 3. **`classList.remove("bar")`**: * Pros: Fast, as it's optimized for class management. * Cons: Requires modern browsers that support the `classList` property. **Library usage** In this benchmark, the following library is used: 1. None explicitly mentioned; however, some browsers (like Safari) might use internal libraries or frameworks to manage classes. **Special JS features/syntax** None are explicitly mentioned in the provided code snippets. However, note that class management methods (`classList.remove`, `removeAttribute`) are supported by modern JavaScript engines and can be used as part of ES6+ code. **Alternatives** If you wanted to compare these approaches with others, consider: 1. Using a more explicit string concatenation approach (e.g., `"class="`) 2. Implementing your own custom class management mechanism 3. Comparing the performance of different DOM manipulation libraries or frameworks Keep in mind that this benchmark is designed to test specific use cases and might not be representative of all scenarios where class management is involved. In summary, the benchmark compares three approaches for removing a class from an HTML element: using the assignment operator (`className = ""`), the `removeAttribute` method, and the `classList.remove` method. The results can help you understand which approach is fastest in your specific use cases.
Related benchmarks:
jQuery removeClass vs classList.remove
classList.remove vs. className.indexOf then remove
jQuery removeClass vs jQuery classList remove
set/removeAttribute, classList with css
Comments
Confirm delete:
Do you really want to delete benchmark?