Utility to report time to interactive(tti) for Real User Monitoring (RUM) for web applications
Contains a promise which on resolution reports the TTI value for the page.
Calling this on an event returns a promise which on resolution reports TTI for that event.
Referential TTI can be fired from the click event, route navigations when you render a component or an event after which you want to check how much is the time to interactive for that action
Basic idea is to make use of LongTask API to see if CPU was busy & report time to interactive based on the number of CPU cycles for which CPU was busy.
- First attempt is to see if PerformanceLongTaskTiming is available in window.
- If it is available we report TTI as reported by tti-polyfill.
- There are instances where TTI reported by tti-polyfill is less than time for loadEventEnd, thus we return max of the two.
- If PerformanceLongTaskTiming is not available we fall back to manual polling to check if CPU is busy.
- As we get a window of 500ms for which CPU was idle, we report TTI based on the number of cycles we had to wait to see an idle 500ms window.
- We look for the idle window after loadEventEnd has happened so that we are also waiting for network idle state.
Including the library
npm i "time-to-interactive";
Consuming the APIs
As it includes polyfill for Promises & tti-polyfill, there is no browser based compatibility matrix as such. Tested with Chrome, Mozilla Firefox, Safari, Edge & IE
A word from the author
The term, time to interactive can be subjective & there could be different ways to approach it. I have tried to get a balance between how Google Chrome's lighthouse does it & how it is done by Akamai's Boomerang. Research & analysis of statstics reported as TTI suggests that, it can vary drastically between browses & platforms. As the LongTaskAPI is yet to be standardized by the browsers, we rely on manual polling to see if CPU was busy. I also recommend usage of another metric which is "first-input-delay", which is quite a standard now.
Author: Gaurav Behere, email@example.com
Feel free to raise a PR, if you see a scope of improvement :)