<div id="el">
<h2>Lorem ipsum dolor sit amet</h2>
</div>
var el = document.getElementById("el");
const { clientHeight } = el;
const perim = (clientHeight) * 2;
const { offsetHeight } = el;
const perim = (offsetHeight) * 2;
const { height } = el.getBoundingClientRect();
const perim = (height) * 2;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
clientHeight | |
offsetHeight | |
getBoundingClientRect |
Test name | Executions per second |
---|---|
clientHeight | 1281814.5 Ops/sec |
offsetHeight | 1310361.8 Ops/sec |
getBoundingClientRect | 837368.9 Ops/sec |
Let's break down the provided JSON benchmark definition and test cases.
Benchmark Definition
The benchmark measures the performance of three different approaches to calculate the perimeter (or circumference) of an HTML element:
clientHeight
: The client height is the distance between the top and bottom edges of the element, excluding any padding or borders.offsetHeight
: The offset height is the total distance from the top edge of the element to its baseline, including any padding or borders.getBoundingClientRect.height
: The getBoundingClientRect
method returns an object with a height
property that represents the current size of the element in pixels.Options Compared
The benchmark compares the performance of each approach on three different browsers (Safari 16) and two device platforms (Desktop).
Pros and Cons of Each Approach
clientHeight
for elements with significant padding or borders.clientHeight
, as it requires accessing multiple properties.getBoundingClientRect
.Library
The benchmark does not use any external libraries. However, it relies on the following JavaScript features:
document.getElementById()
: A method to retrieve an element by its ID.const { property } = el;
: Destructuring assignment to extract a property from an object.Special JS Feature
The benchmark uses the getBoundingClientRect()
method, which is a special JavaScript feature introduced in modern browsers (starting from Firefox 4 and Chrome 16). This method returns an object with various properties that describe the element's position and size relative to the viewport.
Other Alternatives
To replicate this benchmark, you can use the following alternatives:
document.getElementById()
and getBoundingClientRect()
, you could use a library like jQuery or DOMParser to parse the HTML and calculate the element's size.Keep in mind that each alternative has its own trade-offs and challenges. The original benchmark uses native JavaScript features, which provides the most accurate results but requires actual browser support.