<div id="el">
<h2>Lorem ipsum dolor sit amet</h2>
</div>
var el = document.getElementById("el");
const { offsetWidth: width, offsetHeight: height } = el;
const perim = (width + height) * 2;
const { offsetWidth, offsetHeight } = el;
const perim = (offsetWidth + offsetHeight) * 2;
const width = el.offsetWidth;
const height = el.offsetHeight;
const perim = (width + height) * 2;
const { width, height } = el.getBoundingClientRect();
const perim = (width + height) * 2;
const rect = el.getBoundingClientRect();
const width = rect.width;
const height = rect.height;
const perim = (width + height) * 2;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
offsetWidth / offsetHeight (destructured and renamed) | |
offsetWidth / offsetHeight (destructured) | |
offsetWidth / offsetHeight (simple property access) | |
getBoundingClientRect() (destructured) | |
getBoundingClientRect() (simple property access) |
Test name | Executions per second |
---|---|
offsetWidth / offsetHeight (destructured and renamed) | 1977626.4 Ops/sec |
offsetWidth / offsetHeight (destructured) | 2000497.9 Ops/sec |
offsetWidth / offsetHeight (simple property access) | 1961443.1 Ops/sec |
getBoundingClientRect() (destructured) | 2512545.5 Ops/sec |
getBoundingClientRect() (simple property access) | 2646416.5 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark measures the performance of three different ways to calculate the perimeter of an element's bounding box:
offsetWidth / offsetHeight
using destructure (two separate variables)offsetWidth / offsetHeight
using simple property access (one variable for both width and height)getBoundingClientRect()
with simple property access (width and height from a returned object)Comparison Options
getBoundingClientRect()
is generally considered more modern and flexible, as it returns a single object with multiple properties (width, height, top, left, etc.).Library Used
None explicitly mentioned in the benchmark definition, but el
(short for "element") is likely a reference to an HTML element. However, since getBoundingClientRect()
is used, it implies that the browser supports the Element.getBoundingClientRect()
method, which is part of the DOM API.
Special JS Feature/Syntax
The benchmark uses deconstruction syntax (const { width, height } = el;
) and simple property access (el.offsetWidth
), which are modern JavaScript features. The getBoundingClientRect()
method is a more standard way to calculate the bounding box, but it's also been around for a while.
Benchmark Results
The latest results show that:
getBoundingClientRect()
with simple property access is the fastest (averaging 1207.04 executions per second).offsetWidth / offsetHeight
with destructure is slower than the previous method but still faster than simple property access (averaging 1232.20 executions per second).offsetWidth / offsetHeight
with simple property access is the slowest (averaging 1692.10 executions per second).Alternatives
Some alternative approaches to consider:
getBoundingClientRect()
with a separate variable for width and height might be even faster, but it's not tested in this benchmark.