While working around to boost performance for progressive web apps, I came across a new feature Passive Event Listeners
and I find it hard to understand the concept.
What are Passive Event Listeners
and what is the need to have it in our projects?
It enables developers to opt-in to better scroll performance by eliminating the need for scrolling to block on touch and wheel event listeners.
Problem: All modern browsers have a threaded scrolling feature to permit scrolling to run smoothly even when expensive JavaScript is running, but this optimization is partially defeated by the need to wait for the results of any
touchstart
andtouchmove
handlers, which may prevent the scroll entirely by callingpreventDefault()
on the event.Solution:
{passive: true}
By marking a touch or wheel listener as passive, the developer is promising the handler won't call
preventDefault
to disable scrolling. This frees the browser up to respond to scrolling immediately without waiting for JavaScript, thus ensuring a reliably smooth scrolling experience for the user.DOM Spec , Demo Video , Explainer Doc