<div id="foo"></div>
var i = 3000;
while (i--) {
window.getComputedStyle(document.body).getPropertyValue("display").indexOf('none') !== -1
}
var i = 3000;
while (i--) {
document.body.className.includes('hide');
}
var i = 3000;
while (i--) {
document.body.classList.contains('hide');
}
var i = 3000;
const styles = window.getComputedStyle(document.body);
while (i--) {
styles.getPropertyValue("display").includes('none');
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getComputedStyle | |
className | |
classList | |
getComputedStyle cached |
Test name | Executions per second |
---|---|
getComputedStyle | 767.9 Ops/sec |
className | 4984.7 Ops/sec |
classList | 4086.4 Ops/sec |
getComputedStyle cached | 2814.0 Ops/sec |
Benchmark Overview
The provided benchmark, window.getComputedStyle vs. className vs. classList 2
, measures the performance of three different approaches to check if an element has a specific CSS class: window.getComputedStyle
, className
, and classList
. The test is designed to simulate a scenario where an application needs to frequently check if an element's display style is set to "none", which can be achieved by checking for the presence of the "hide" class.
Approaches Compared
window.getComputedStyle
: This method retrieves the computed style of an element, and then checks if the display
property has a value of "none"
using the indexOf
method.className
: This method returns all classes currently applied to an element as a single string, separated by spaces. The test checks if the "hide" class is present in this string using the includes
method.classList
: This method provides a more modern alternative to className
, returning an array-like object of classes applied to an element. The test uses the contains
method to check if the "hide" class is present.Pros and Cons
window.getComputedStyle
:display
). However, this approach requires parsing the computed style string, which can be slower.getComputedStyle
method, which may have additional overhead due to its asynchronous nature and potential DOM traversal costs.className
:classList
:className
, with better performance characteristics. It returns an array-like object of classes, allowing for faster membership checks using the contains
method.Library and Special JS Feature
The test uses the classList
property, which is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). This property provides an array-like interface to access and manipulate class names of elements. While this approach offers better performance, it may require additional support or polyfills for older browsers.
Other Considerations
getComputedStyle
method involves accessing the DOM, which can incur additional overhead due to tree traversal costs.Alternatives
If you need to compare other approaches or consider alternative solutions, here are a few options:
window.getComputedStyle
, you could use the CSS Object Model (CSSOM) API to access and manipulate styles directly. This would eliminate the need for parsing computed style strings.Element.matches()
or Element.contains()
, which provide more efficient membership checks compared to string-based approaches.When choosing a solution, consider factors such as performance requirements, browser support, and code maintainability.