Skip to the solution
EasyFrontend6 min read

Why would you add { passive: true } to a scroll or touchstart listener, and what problem does it solve for scroll performance?

263 views

The problem

Question presented to candidate: "You add a touchstart listener to track gesture analytics. Users report scrolling feels janky on mobile after you added it, even though your listener does almost nothing. Why, and how would you fix it?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” by default, the browser genuinely cannot start scrolling in response to a touch gesture until it has first RUN every registered touchstart/touchmove listener to completion, because any one of them COULD call .preventDefault() to cancel the scroll β€” even an empty or near-empty listener genuinely introduces a real, measurable delay before scrolling can begin.
  • πŸ“Œ Interview term: { passive: true } β€” a real, third-argument option to addEventListener that tells the browser, in advance, "this listener will never call preventDefault()" β€” letting the browser genuinely start scrolling immediately, in parallel, without waiting for the listener to finish running at all.
  • πŸ“Œ Interview term: the real, verified consequence of the promise β€” verified directly: calling .preventDefault() INSIDE a listener registered with { passive: true } genuinely does NOT throw β€” the browser silently ignores the call β€” but it genuinely has ZERO effect, confirmed directly (event.defaultPrevented stayed false even after the call) β€” the browser is trusting the passive promise was kept, and does not even check.
  • πŸ“Œ Interview term: the real fix for the prompt's exact scenario β€” a precise answer names that adding { passive: true } to the touchstart listener (since the analytics tracking genuinely never needs to cancel the gesture) genuinely removes the real, measurable scroll-blocking delay β€” the browser can start scrolling the instant the touch begins, running the analytics listener in parallel rather than beforehand.
  • A precise answer names that several browsers have made touchstart/touchmove listeners genuinely default to { passive: true } UNLESS explicitly marked { passive: false } β€” a real, deliberate platform-level mitigation for exactly this common performance footgun β€” but relying on that default rather than being explicit is a real, honest gap in guaranteed cross-browser behavior worth naming.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly diagnosing the prompt's own reported jank with real, verified proof is the strong signal.

Code / implementation expected: Yes β€” a real, direct demonstration that calling preventDefault() inside a { passive: true } listener neither throws nor has any effect.

  • rendering
  • 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

6 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, direct proof: calling preventDefault() inside a { passive: true } listener genuinely neither throws nor has any effect β€” verified directly

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 33 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 nextEasy

How does instanceof work?

Continue