<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).getBoundingClientRect().width === 0;
}
var i = 5000;
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return document.getElementById(id).clientHeight === 0;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getComputedStyle | |
getBoundingClientRect | |
clientheight |
Test name | Executions per second |
---|---|
getComputedStyle | 213.1 Ops/sec |
getBoundingClientRect | 368.7 Ops/sec |
clientheight | 817.7 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Benchmark Definition
The benchmark measures the performance of three different ways to check if an element is invisible: window.getComputedStyle
, document.getElementById().getBoundingClientRect()
, and document.getElementById().clientHeight
.
In each test case, a loop is executed 5000 times, checking the display status of an HTML element with the id "foo" using each of the three methods.
Options Compared
The three options being compared are:
Pros and Cons
window.getComputedStyle
since it only returns a simple rectangle object.Libraries and Special JS Features
None of the test cases use any external libraries. However, they do utilize some special JavaScript features:
Other Alternatives
Some alternative approaches could be:
display
property directly: Instead of using window.getComputedStyle
, you could simply check if the display
property is set to "none"
.offsetHeight
and offsetWidth
: Similar to clientHeight
, but takes into account any padding or border that may affect the element's size.Keep in mind that these alternatives might have different performance characteristics compared to the original benchmark.