A common performance issue in web apps is forced synchronous layout: reading the size of an element and forcing the browser to immediately and synchronously undergo layout and style calculations in order to figure out the size.
This can be worked around by using a ResizeObserver. This API lets you receive a callback whenever the browser changes the size of an observed element. When the callback is invoked, the size of the element has just been calculated. This means you can safely read properties like .offsetWidth
, .scrollWidth
, and .clientWidth
in addition to the properties provided by the ResizeObserver callback like .borderBoxSize
. (As long as you don’t invalidate it by writing to those properties before reading them.)
This is a useful alternative to other ways to avoid forced synchronous layout, such as using requestAnimationFrame
.