<div id="foo"></div>
var i = 5000;
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return window.getComputedStyle(document.getElementById(id), null).getPropertyValue("display") === "none";
}
var i = 5000;
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return document.getElementById(id).getClientRects().width === 0;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getComputedStyle | |
getClientRects |
Test name | Executions per second |
---|---|
getComputedStyle | 400.9 Ops/sec |
getClientRects | 212.9 Ops/sec |
Let's dive into the explanation.
Benchmark Purpose
The benchmark tests two approaches to determine if an HTML element is hidden (i.e., its display property is set to "none").
Approaches Compared
window.getComputedStyle(document.getElementById(id), null).getPropertyValue("display") === "none"
document.getElementById(id).getClientRects().width === 0
Pros and Cons of Each Approach:
window.getComputedStyle
approach:getClientRects
.getClientRects
approach:getClientRects
is not supported in older browsers).Library Used
None. This benchmark does not use any external libraries.
Special JS Feature/ Syntax
None mentioned in this specific benchmark, but note that some modern JavaScript features and syntax might be used in other benchmarks.
Other Alternatives
If you wanted to test a different approach, you could consider:
element.style.display === "none"
: This checks the element's CSS style directly.document.querySelector('#foo').style.display === 'none'
: This uses the querySelector API to select the element and then checks its display style.Keep in mind that these alternatives might have similar pros and cons as the approaches tested in this benchmark.
I hope this explanation helps!