Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
window.getComputedStyle vs. className vs. classList
(version: 0)
Comparing performance of:
getComputedStyle vs className vs classList
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
getComputedStyle
var i = 3000; while (i--) { window.getComputedStyle(document.body).getPropertyValue("display").indexOf('none') !== -1 }
className
var i = 3000; while (i--) { document.body.className.indexOf('hide') !== -1; }
classList
var i = 3000; while (i--) { document.body.classList.contains('hide'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
getComputedStyle
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 dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmark test that compares three approaches to check if an HTML element has a certain class or display property: 1. `window.getComputedStyle(document.body).getPropertyValue("display").indexOf('none') !== -1` 2. `document.body.className.indexOf('hide') !== -1` 3. `document.body.classList.contains('hide')` Let's break down each option and its pros and cons: **Option 1: window.getComputedStyle** This approach uses the `getComputedStyle` method to retrieve the computed style of the `body` element and then checks if the `display` property is set to `'none'`. This method can be slow because it involves retrieving the entire style sheet and parsing it. Pros: Can work on elements with complex styles, including CSS variables. Cons: * Slow due to the overhead of retrieving and parsing the style sheet. * May not be suitable for simple cases where the display property is set directly in CSS. **Option 2: className** This approach uses the `className` property of the `body` element to check if it contains a specific class name, `'hide'`. This method is relatively fast because it only checks the class names without parsing the entire style sheet. Pros: * Fast because it only involves checking class names. * Suitable for simple cases where the display property is set directly in CSS. Cons: May not work correctly with CSS variables or other complex styles. **Option 3: classList** This approach uses the `classList` property of the `body` element to check if it contains a specific class name, `'hide'`. This method is similar to `className`, but it provides more features and flexibility. Pros: * Fast because it only involves checking class names. * Provides additional features like adding or removing classes dynamically. Cons: May have slight performance overhead compared to `className`. Now, let's talk about the libraries used in these tests. None of the tests use any external libraries. However, if we were to extend this test to support other platforms or browsers, we might consider using a library like [jsdom](https://github.com/jsdom/jsdom) to simulate the DOM and provide a more robust testing environment. Regarding special JavaScript features or syntax, none are mentioned in these tests. The benchmarks focus on comparing three simple approaches to check class existence. Now, let's take a look at other alternatives for similar benchmarks: * For a benchmark that compares parsing strings with using a regular expression, you might consider using a library like [fast-assert](https://github.com/kevlinhenry/fast-assert). * For a benchmark that compares the performance of different DOM traversal methods (e.g., `querySelectorAll`, `getElementsByClassName`, etc.), you might consider using a library like [jsdom](https://github.com/jsdom/jsdom). These are just a few examples, and the choice of alternative will depend on the specific requirements and goals of your benchmark.
Related benchmarks:
jquery.css vs getComputedStyle
jquery3.4.1.css vs getComputedStyle
window.getComputedStyle (no props requested) vs. classList.contains
window.getComputedStyle vs. className vs. classList 2
Comments
Confirm delete:
Do you really want to delete benchmark?