<div id="teste">teste</div>
var teste_n = '123px';
var _style = window.getComputedStyle(document.getElementById('teste'));
var el = document.getElementById('teste');
document.getElementById('teste').clientWidth;
document.getElementById('teste').offsetwidth;
parseFloat(_style['width'])
el.getBoundingClientRect()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
clientWidth | |
offsetwidth | |
window.getComputedStyle | |
BoundingClientRect |
Test name | Executions per second |
---|---|
clientWidth | 1829593.9 Ops/sec |
offsetwidth | 9064632.0 Ops/sec |
window.getComputedStyle | 1170574.9 Ops/sec |
BoundingClientRect | 1549144.4 Ops/sec |
Let's dive into the details of this benchmark.
What is being tested?
The benchmark compares four different methods to get the width of an HTML element:
clientWidth
offsetwidth
(Note: There is no such property as "offsetwidth". I assume it's a typo, and it should be offsetWidth
)window.getComputedStyle
with parsing its resultgetBoundingClientRect
What options are compared?
These four methods are being compared to determine which one is the fastest (measured in executions per second) on a specific browser and device platform.
Pros/Cons of each approach:
Library and special JS features/syntax:
None are used in this benchmark.
Other considerations:
When choosing between these methods, consider the following:
clientWidth
.offsetWidth
or getBoundingClientRect
.window.getComputedStyle
.Other alternatives:
You can also consider using CSS's width
property directly on the element, which can be faster than accessing the computed styles.
In summary, this benchmark compares four methods to get the width of an HTML element and helps determine which one is the fastest for a specific browser and device platform.