Skip to content

Instantly share code, notes, and snippets.

@moeriki
Created April 21, 2023 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moeriki/462ffc07603a922b67db6845f402e709 to your computer and use it in GitHub Desktop.
Save moeriki/462ffc07603a922b67db6845f402e709 to your computer and use it in GitHub Desktop.
var t,e=function(){return window.performance&&(performance.getEntriesByType&&performance.getEntriesByType("navigation")[0]||function(){var t=performance.timing,e=performance.navigation.type,n={entryType:"navigation",startTime:0,type:2==e?"back_forward":1===e?"reload":"navigate"};for(var r in t)"navigationStart"!==r&&"toJSON"!==r&&(n[r]=Math.max(t[r]-t.navigationStart,0));return n}())},n=function(t){if("loading"===document.readyState)return"loading";var n=e();if(n){if(t<n.domInteractive)return"loading";if(0===n.domContentLoadedEventStart||t<n.domContentLoadedEventStart)return"dom-interactive";if(0===n.domComplete||t<n.domComplete)return"dom-content-loaded"}return"complete"},r=function(t){var e=t.nodeName;return 1===t.nodeType?e.toLowerCase():e.toUpperCase().replace(/^#/,"")},i=function(t,e){var n="";try{for(;t&&9!==t.nodeType;){var i=t,a=i.id?"#"+i.id:r(i)+(i.className&&i.className.trim().length?"."+i.className.trim().replace(/\s+/g,"."):"");if(n.length+a.length>(e||100)-1)return n||a;if(n=n?a+">"+n:a,i.id)break;t=i.parentNode}}catch(t){}return n},a=-1,o=function(){return a},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(a=e.timeStamp,t(e))}),!0)},c=function(){var t=e();return t&&t.activationStart||0},s=function(t,n){var r=e(),i="navigate";return o()>=0?i="back-forward-cache":r&&(document.prerendering||c()>0?i="prerender":document.wasDiscarded?i="restore":r.type&&(i=r.type.replace(/_/g,"-"))),{name:t,value:void 0===n?-1:n,rating:"good",delta:0,entries:[],id:"v3-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},f=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},d=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},l=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},m=function(t){var e=function(e){"pagehide"!==e.type&&"hidden"!==document.visibilityState||t(e)};addEventListener("visibilitychange",e,!0),addEventListener("pagehide",e,!0)},v=function(t){var e=!1;return function(n){e||(t(n),e=!0)}},p=-1,h=function(t){"hidden"===document.visibilityState&&p>-1&&(p="visibilitychange"===t.type?t.timeStamp:0,y())},g=function(){addEventListener("visibilitychange",h,!0),addEventListener("prerenderingchange",h,!0)},y=function(){removeEventListener("visibilitychange",h,!0),removeEventListener("prerenderingchange",h,!0)},T=function(){return p<0&&((p=window.webVitals.firstHiddenTime)===1/0&&g(),u((function(){setTimeout((function(){p="hidden"!==document.visibilityState||document.prerendering?1/0:0,g()}),0)}))),{get firstHiddenTime(){return p}}},S=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},b=[1800,3e3],E=function(t,e){e=e||{},S((function(){var n,r=T(),i=s("FCP"),a=f("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTime<r.firstHiddenTime&&(i.value=Math.max(t.startTime-c(),0),i.entries.push(t),n(!0)))}))}));a&&(n=d(t,i,b,e.reportAllChanges),u((function(r){i=s("FCP"),n=d(t,i,b,e.reportAllChanges),l((function(){i.value=performance.now()-r.timeStamp,n(!0)}))})))}))},w=[.1,.25],C=function(t,e){!function(t,e){e=e||{},E(v((function(){var n,r=s("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},c=f("layout-shift",o);c&&(n=d(t,r,w,e.reportAllChanges),m((function(){o(c.takeRecords()),n(!0)})),u((function(){i=0,r=s("CLS",0),n=d(t,r,w,e.reportAllChanges),l((function(){return n()}))})),setTimeout(n,0))})))}((function(e){!function(t){if(t.entries.length){var e=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(e&&e.sources&&e.sources.length){var r=(a=e.sources).find((function(t){return t.node&&1===t.node.nodeType}))||a[0];if(r)return void(t.attribution={largestShiftTarget:i(r.node),largestShiftTime:e.startTime,largestShiftValue:e.value,largestShiftSource:r,largestShiftEntry:e,loadState:n(e.startTime)})}}var a;t.attribution={}}(e),t(e)}),e)},L=function(t,r){E((function(r){!function(t){if(t.entries.length){var r=e(),i=t.entries[t.entries.length-1];if(r){var a=r.activationStart||0,u=Math.max(0,r.responseStart-a);return void(t.attribution={timeToFirstByte:u,firstByteToFCP:t.value-u,loadState:n(t.entries[0].startTime),navigationEntry:r,fcpEntry:i})}}t.attribution={timeToFirstByte:0,firstByteToFCP:t.value,loadState:n(o())}}(r),t(r)}),r)},M=[100,300],x=function(t,e){!function(t,e){e=e||{},S((function(){var n,r=T(),i=s("FID"),a=function(t){t.startTime<r.firstHiddenTime&&(i.value=t.processingStart-t.startTime,i.entries.push(t),n(!0))},o=function(t){t.forEach(a)},c=f("first-input",o);n=d(t,i,M,e.reportAllChanges),c&&m(v((function(){o(c.takeRecords()),c.disconnect()}))),console.warn('The web-vitals "base+polyfill" build is deprecated. See: https://bit.ly/3aqzsGm'),c||window.webVitals.firstInputPolyfill(a),u((function(){i=s("FID"),n=d(t,i,M,e.reportAllChanges),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(a)}))}))}((function(e){!function(t){var e=t.entries[0];t.attribution={eventTarget:i(e.target),eventType:e.name,eventTime:e.startTime,eventEntry:e,loadState:n(e.startTime)}}(e),t(e)}),e)},F=0,I=1/0,A=0,P=function(t){t.forEach((function(t){t.interactionId&&(I=Math.min(I,t.interactionId),A=Math.max(A,t.interactionId),F=A?(A-I)/7+1:0)}))},B=function(){return t?F:performance.interactionCount||0},k=function(){"interactionCount"in performance||t||(t=f("event",P,{type:"event",buffered:!0,durationThreshold:0}))},D=[200,500],N=0,R=function(){return B()-N},q=[],H={},V=function(t){var e=q[q.length-1],n=H[t.interactionId];if(n||q.length<10||t.duration>e.latency){if(n)n.entries.push(t),n.latency=Math.max(n.latency,t.duration);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};H[r.id]=r,q.push(r)}q.sort((function(t,e){return e.latency-t.latency})),q.splice(10).forEach((function(t){delete H[t.id]}))}},O=function(t,e){e=e||{},S((function(){k();var n,r=s("INP"),i=function(t){t.forEach((function(t){(t.interactionId&&V(t),"first-input"===t.entryType)&&(!q.some((function(e){return e.entries.some((function(e){return t.duration===e.duration&&t.startTime===e.startTime}))}))&&V(t))}));var e,i=(e=Math.min(q.length-1,Math.floor(R()/50)),q[e]);i&&i.latency!==r.value&&(r.value=i.latency,r.entries=i.entries,n())},a=f("event",i,{durationThreshold:e.durationThreshold||40});n=d(t,r,D,e.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),m((function(){i(a.takeRecords()),r.value<0&&R()>0&&(r.value=0,r.entries=[]),n(!0)})),u((function(){q=[],N=B(),r=s("INP"),n=d(t,r,D,e.reportAllChanges)})))}))},_=function(t,e){O((function(e){!function(t){if(t.entries.length){var e=t.entries.sort((function(t,e){return e.duration-t.duration||e.processingEnd-e.processingStart-(t.processingEnd-t.processingStart)}))[0];t.attribution={eventTarget:i(e.target),eventType:e.name,eventTime:e.startTime,eventEntry:e,loadState:n(e.startTime)}}else t.attribution={}}(e),t(e)}),e)},j=[2500,4e3],z={},G=function(t,n){!function(t,e){e=e||{},S((function(){var n,r=T(),i=s("LCP"),a=function(t){var e=t[t.length-1];e&&e.startTime<r.firstHiddenTime&&(i.value=Math.max(e.startTime-c(),0),i.entries=[e],n())},o=f("largest-contentful-paint",a);if(o){n=d(t,i,j,e.reportAllChanges);var p=v((function(){z[i.id]||(a(o.takeRecords()),o.disconnect(),z[i.id]=!0,n(!0))}));["keydown","click"].forEach((function(t){addEventListener(t,p,!0)})),m(p),u((function(r){i=s("LCP"),n=d(t,i,j,e.reportAllChanges),l((function(){i.value=performance.now()-r.timeStamp,z[i.id]=!0,n(!0)}))}))}}))}((function(n){!function(t){if(t.entries.length){var n=e();if(n){var r=n.activationStart||0,a=t.entries[t.entries.length-1],o=a.url&&performance.getEntriesByType("resource").filter((function(t){return t.name===a.url}))[0],u=Math.max(0,n.responseStart-r),c=Math.max(u,o?(o.requestStart||o.startTime)-r:0),s=Math.max(c,o?o.responseEnd-r:0),f=Math.max(s,a?a.startTime-r:0),d={element:i(a.element),timeToFirstByte:u,resourceLoadDelay:c-u,resourceLoadTime:s-c,elementRenderDelay:f-s,navigationEntry:n,lcpEntry:a};return a.url&&(d.url=a.url),o&&(d.lcpResourceEntry=o),void(t.attribution=d)}}t.attribution={timeToFirstByte:0,resourceLoadDelay:0,resourceLoadTime:0,elementRenderDelay:t.value}}(n),t(n)}),n)},J=[800,1800],U=function t(e){document.prerendering?S((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},K=function(t,n){n=n||{};var r=s("TTFB"),i=d(t,r,J,n.reportAllChanges);U((function(){var a=e();if(a){var o=a.responseStart;if(o<=0||o>performance.now())return;r.value=Math.max(o-c(),0),r.entries=[a],i(!0),u((function(){r=s("TTFB",0),(i=d(t,r,J,n.reportAllChanges))(!0)}))}}))},Q=function(t,e){K((function(e){!function(t){if(t.entries.length){var e=t.entries[0],n=e.activationStart||0,r=Math.max(e.domainLookupStart-n,0),i=Math.max(e.connectStart-n,0),a=Math.max(e.requestStart-n,0);t.attribution={waitingTime:r,dnsTime:i-r,connectionTime:a-i,requestTime:t.value-a,navigationEntry:e}}else t.attribution={waitingTime:0,dnsTime:0,connectionTime:0,requestTime:0}}(e),t(e)}),e)};export{w as CLSThresholds,b as FCPThresholds,M as FIDThresholds,D as INPThresholds,j as LCPThresholds,J as TTFBThresholds,C as onCLS,L as onFCP,x as onFID,_ as onINP,G as onLCP,Q as onTTFB};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment