<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;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getComputedStyle | |
getBoundingClientRect |
Test name | Executions per second |
---|---|
getComputedStyle | 419.9 Ops/sec |
getBoundingClientRect | 863.2 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark definition provides two options for comparison:
window.getComputedStyle(document.getElementById(id), null).getPropertyValue("display") === "none"
document.getElementById(id).getBoundingClientRect().width === 0
These options are essentially checking whether an HTML element with the id "foo" has its display property set to none or if its width is zero, respectively.
Pros and Cons of Different Approaches
window.getComputedStyle
approach: This method uses the Web API to retrieve the computed style properties of an element. It's a more comprehensive way to get all the styles applied to an element, including layout-specific styles like display.
Pros: More accurate, especially for elements with complex layouts.
Cons: Can be slower due to the overhead of getting all the styles and then filtering them out.
getBoundingClientRect
approach: This method uses the Web API to get the bounding rectangle of an element, which includes its width, height, left, top, and more. By checking if the width is zero, it can be used as a proxy for checking if the display property is none.
Pros: Faster than window.getComputedStyle
since it only returns specific properties.
Cons: Does not account for layout-specific styles like display, which could lead to false negatives.
Library and Its Purpose
In both test cases, the checkDisplay
function uses the document.getElementById(id)
method to retrieve an element by its id. This is a standard JavaScript API that allows accessing HTML elements by their id attribute.
No specific library is being used in this benchmark, as it's a basic comparison between two built-in Web APIs.
Special JS Features or Syntax
None of the code uses special JavaScript features or syntax beyond what's covered by the standard Web APIs.
Other Alternatives
If you wanted to compare these methods more thoroughly, you could also consider using other approaches:
getComputedStyle
with a specific property: Instead of checking display
, you could check other properties like width
, height
, etc.getBoundingClientRect
with multiple values: You could use multiple values for the width to check for different display states (e.g., 0, 10, and 100).window.getComputedStyle
.chrome-devtools-protocol
or perf_hooks
to get more detailed performance metrics.Keep in mind that these alternatives would add complexity to the benchmark and might not provide significant improvements for this specific test case.