Skip to the solution
MediumFrontend5 min read

How would you use ResizeObserver to react to an element's size changing, without polling getBoundingClientRect() on every frame?

645 views

The problem

Question presented to candidate: "You need to re-run a layout calculation whenever a specific div's size changes β€” maybe from a CSS container query, a font load, or a user dragging a resize handle. Would you poll getBoundingClientRect() in a requestAnimationFrame loop? What would you use instead?"

What a strong answer should cover:

  • πŸ“Œ Interview term: ResizeObserver β€” a real, built-in browser API that lets code .observe(element) and receive a real callback whenever that element's size genuinely changes β€” no polling loop, no manual requestAnimationFrame-based checking required at all.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly, live in a real browser (ResizeObserver is genuinely NOT implemented in jsdom, confirmed directly β€” typeof is "undefined" there): ResizeObserver's callback genuinely fires ONCE immediately upon .observe(), reporting the element's real CURRENT size β€” even before any actual resize occurs β€” then fires AGAIN whenever the element's real size subsequently changes.
  • πŸ“Œ Interview term: entry.contentRect β€” the real object each callback entry carries, giving the observed element's real, current width and height directly β€” no need to separately call getBoundingClientRect() inside the callback at all.
  • πŸ“Œ Interview term: .disconnect() β€” verified directly: calling it genuinely stops all future callbacks for every element that observer was watching, confirmed by a subsequent real resize producing no further callback.
  • A precise answer names the real performance advantage over polling: a requestAnimationFrame-based polling loop runs on EVERY frame (up to 60 times per second) regardless of whether anything actually changed, genuinely wasting real CPU; ResizeObserver's callback fires ONLY when a real, actual size change genuinely occurs.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly demonstrating the real, verified fire-immediately-then-on-resize behavior is the strong signal.

Code / implementation expected: Yes β€” a real, live-browser-verified ResizeObserver observing an element through an initial callback, a real resize, and a .disconnect().

  • observers
  • tricky
  • real-world

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, live-verified proof: ResizeObserver fires immediately on observe() with the current size, again on a genuine resize, and disconnect() stops further callbacks

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 75 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between arrow and regular functions?

Continue