Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
offsetWidth vs getBoundingClientRect1
(version: 1)
Comparing performance of: offsetwidth vs getBoundingClientRect
Comparing performance of:
offsetWidth vs getBoundingClientRect
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="test">test</div>
Script Preparation code:
var test_n = '123px'; var _style = window.getComputedStyle(document.getElementById('test'));
Tests:
offsetWidth
document.getElementById('test').offsetWidth;
getBoundingClientRect
document.getElementById('test').getBoundingClientRect().width;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
offsetWidth
getBoundingClientRect
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
offsetWidth
1453534.2 Ops/sec
getBoundingClientRect
958369.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark, we are comparing the performance of two different methods for obtaining the width of a DOM element in JavaScript: `offsetWidth` and `getBoundingClientRect`. ### Tested Options 1. **offsetWidth** - **Benchmark Definition**: `document.getElementById('test').offsetWidth;` - **Description**: This property returns the layout width of the element, including padding but not the margin. It is a direct measurement and typically provides a fast way to acquire the width. 2. **getBoundingClientRect** - **Benchmark Definition**: `document.getElementById('test').getBoundingClientRect().width;` - **Description**: This method returns a `DOMRect` object providing the size of the element and its position relative to the viewport. The width property of the `DOMRect` is what we are measuring here. ### Pros and Cons #### offsetWidth - **Pros**: - Simplicity: `offsetWidth` is straightforward to use and understand. - Performance: In many cases, accessing it can be faster (as observed in the benchmark results) because it directly retrieves the information from the element's rendered properties. - **Cons**: - Limited Information: `offsetWidth` offers only the width, without information about the position relative to the viewport or any transformation effects. #### getBoundingClientRect - **Pros**: - Comprehensive: Provides more information than just width, including the position of the element, which can be helpful in some scenarios (such as animations or UI adjustments). - Accurate: Takes into account transformations (like scaling and rotation) that might affect the rendered size in your layout. - **Cons**: - Potentially Slower: As seen in the benchmark, while it's versatile, it can be slower compared to `offsetWidth` since it computes the rectangle including layout adjustments. ### Other Considerations - **Performance Impact**: The performance difference is particularly relevant in scenarios where you may need to measure widths in a loop or for a large number of elements, as it can significantly affect application performance. - **Use Cases**: Use `offsetWidth` when you need only the width and performance is critical, while `getBoundingClientRect` should be used when you need both size and position on the screen. ### Alternative Approaches Other methods to measure widths include: - **clientWidth**: Similar to `offsetWidth`, but excludes borders and scrollbars. It is helpful if you need the inner width for a box-model calculation. - **getComputedStyle()**: Though it's primarily used to get CSS styles, it can be employed to read the width string and convert it to a numeric value. This method involves more overhead, especially on layouts since it requires extra parsing and can lead to layout thrashing. ### Conclusion The benchmark provides a clear comparison of two common methods for measuring element width in JavaScript. This knowledge is crucial for developers focused on optimizing performance in web applications while considering accuracy and the specific context of measurement needs.
Related benchmarks:
clientHeight vs offsetHeight vs window.getComputedStyle
clientWidth vs offsetWidth vs window.getComputedStyle
offsetWidth vs closest
offsetwidth vs getBoundingClientRect
offsetwidth/getBoundingClientRect
clientWidth vs offsetWidth vs window.getComputedStyle vs getBoundingClientRect
clientWidth vs offsetWidth vs window.getComputedStyle v2
clientWidth vs offsetWidth vs scrollWidth
clientWidth vs offsetWidth vs style.width
Comments
Confirm delete:
Do you really want to delete benchmark?