<div id="foo"></div>
var i = 5000;
const node = document.getElementById('foo')
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return parseFloat(window.getComputedStyle(node).height) > 0;
}
var i = 5000;
const node = document.getElementById('foo')
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return node.getBoundingClientRect().height > 0;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getComputedStyle | |
getBoundingClientRect |
Test name | Executions per second |
---|---|
getComputedStyle | 193.8 Ops/sec |
getBoundingClientRect | 360.3 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark measures the performance of two methods to retrieve the height of an HTML element: window.getComputedStyle
and node.getBoundingClientRect
. Both methods are used to get the height of an element, but they have different approaches and characteristics.
Options Compared
The benchmark compares two options:
window.getComputedStyle
: This method retrieves the computed style properties of an element, including its height. It returns a CSSStyleDeclaration object that contains all the styles applied to the element.node.getBoundingClientRect
: This method returns a rectangle object that represents the size and position of an element in the DOM tree. The height
property of this rectangle is used to retrieve the element's height.Pros and Cons
window.getComputedStyle
:getBoundingClientRect
because it needs to fetch additional data from the browser's style cache.node.getBoundingClientRect
:getComputedStyle
, as it only retrieves the element's size and position.Library and Purpose
In this benchmark, the checkDisplay
function uses two libraries:
window.getComputedStyle
: This method is part of the W3C's CSSOM (CSS Object Model) specification, which provides a standardized API for working with styles in web pages.node.getBoundingClientRect
: This method is also part of the DOM (Document Object Model) specification, which defines how elements are represented and manipulated in HTML documents.Special JS Features or Syntax
There is no special JavaScript feature or syntax used in this benchmark beyond what's standard for modern browsers.
Other Alternatives
In addition to window.getComputedStyle
and node.getBoundingClientRect
, other methods can be used to retrieve an element's height, such as:
element.offsetHeight
: This method returns the element's offset height (the height of its content area), excluding any borders or padding.element.outerHeight
: This method returns the element's outer height (including all borders and padding).element.style.height
: This method returns the element's inline style property for the height
attribute, which can be used to set the element's height programmatically.Keep in mind that these alternatives may have different performance characteristics and use cases compared to window.getComputedStyle
and node.getBoundingClientRect
.