// snippets/Loading/Event-Processing-Time.js | sha256:33aaf72d7b92de16 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Event-Processing-Time.js (()=>{const t=[{name:"Redirect",start:"redirectStart",end:"redirectEnd",icon:"🔄"},{name:"DNS Lookup",start:"domainLookupStart",end:"domainLookupEnd",icon:"🔍"},{name:"TCP Connection",start:"connectStart",end:"connectEnd",icon:"🔌"},{name:"SSL/TLS",start:"secureConnectionStart",end:"connectEnd",icon:"🔒"},{name:"Request",start:"requestStart",end:"responseStart",icon:"📤"},{name:"Response",start:"responseStart",end:"responseEnd",icon:"📥"},{name:"DOM Processing",start:"responseEnd",end:"domContentLoadedEventStart",icon:"🏗️"},{name:"DOMContentLoaded",start:"domContentLoadedEventStart",end:"domContentLoadedEventEnd",icon:"📄"},{name:"Resource Loading",start:"domContentLoadedEventEnd",end:"loadEventStart",icon:"📦"},{name:"Load Event",start:"loadEventStart",end:"loadEventEnd",icon:"✅"}];new PerformanceObserver(e=>{const[n]=e.getEntries();if(!n.loadEventEnd)return;const o=t.map(t=>{const e=n[t.start]||0,o=n[t.end]||0;let a=o-e;return"SSL/TLS"===t.name&&n.secureConnectionStart>0?a=n.connectEnd-n.secureConnectionStart:"TCP Connection"===t.name&&n.secureConnectionStart>0&&(a=n.secureConnectionStart-n.connectStart),{...t,start:e,end:o,duration:Math.max(0,a)}}).filter(t=>t.duration>0||"Redirect"===t.name),a=n.loadEventEnd-n.startTime;let r,d;a<=2500?(r="Fast",d="#22c55e"):a<=4e3?(r="Moderate",d="#f59e0b"):(r="Slow",d="#ef4444"),o.forEach(({duration:t})=>{const e=t/a*100;var n;((t,e)=>{const n=Math.min(Math.round((e>0?t/e*100:0)/2.5),40);"█".repeat(n),"░".repeat(Math.max(0,20-n))})(t,a),(n=t,n.toFixed(2)+" ms").padStart(12),`${e.toFixed(1)}%`.padStart(7);let o="";t>.3*a?o="color: #ef4444;":t>.15*a&&(o="color: #f59e0b;")}),o.reduce((t,e)=>t.duration>e.duration?t:e)}).observe({type:"navigation",buffered:!0});const[e]=performance.getEntriesByType("navigation");if(!e)return{script:"Event-Processing-Time",status:"error",error:"No navigation entry"};if(!e.loadEventEnd)return{script:"Event-Processing-Time",status:"tracking",message:"The page is still loading. Run this snippet again after the load event."};const n=e.loadEventEnd-e.startTime,o=(e.responseEnd||0)-e.startTime,a=n<=2500?"good":n<=4e3?"needs-improvement":"poor",r=[{key:"redirect",duration:Math.max(0,e.redirectEnd-e.redirectStart)},{key:"dnsLookup",duration:Math.max(0,e.domainLookupEnd-e.domainLookupStart)},{key:"tcpConnection",duration:Math.max(0,(e.secureConnectionStart>0?e.secureConnectionStart:e.connectEnd)-e.connectStart)},{key:"sslTls",duration:e.secureConnectionStart>0?Math.max(0,e.connectEnd-e.secureConnectionStart):0},{key:"request",duration:Math.max(0,e.responseStart-e.requestStart)},{key:"response",duration:Math.max(0,e.responseEnd-e.responseStart)},{key:"domProcessing",duration:Math.max(0,e.domContentLoadedEventStart-e.responseEnd)},{key:"domContentLoaded",duration:Math.max(0,e.domContentLoadedEventEnd-e.domContentLoadedEventStart)},{key:"resourceLoading",duration:Math.max(0,e.loadEventStart-e.domContentLoadedEventEnd)},{key:"loadEvent",duration:Math.max(0,e.loadEventEnd-e.loadEventStart)}],d=r.filter(t=>t.duration>0),s=d.length>0?d.reduce((t,e)=>t.duration>e.duration?t:e):r[0];return{script:"Event-Processing-Time",status:"ok",value:Math.round(n),unit:"ms",rating:a,thresholds:{good:2500,needsImprovement:4e3},details:{networkTimeMs:Math.round(o),processingTimeMs:Math.round(n-o),ttfbMs:Math.round(Math.max(0,e.responseStart-(e.activationStart||0))),domContentLoadedMs:Math.round(e.domContentLoadedEventEnd),loadCompleteMs:Math.round(e.loadEventEnd),phases:Object.fromEntries(r.map(t=>[t.key,{value:Math.round(t.duration),unit:"ms"}])),slowestPhase:s.key}}})();