@@ -109,10 +109,22 @@ <h1 class="text-3xl md:text-4xl font-bold text-gray-900">Resource Timing API Dem
109109 </ div >
110110
111111 < script >
112+ function bothZeroOrUndefined ( a , b ) {
113+ return ( ( a === undefined ) || ( a === 0.0 ) ) && ( ( b === undefined ) || ( b === 0.0 ) ) ;
114+ }
112115 // Check if the PerformanceObserver API is supported
113116 if ( 'PerformanceObserver' in window ) {
114117 const timingTableBody = document . getElementById ( 'timingTableBody' ) ;
115118
119+ // Function to format timing data for display
120+ const formatTiming = ( start , end ) => {
121+ if ( start === undefined || end === undefined || ( start === 0 && end === 0 ) ) {
122+ return `n/a` ;
123+ }
124+ const duration = ( end - start ) . toFixed ( 2 ) ;
125+ return `${ start . toFixed ( 2 ) } to ${ end . toFixed ( 2 ) } (${ duration } )` ;
126+ } ;
127+
116128 // Function to process performance entries and update the table
117129 const processEntries = ( entries ) => {
118130 entries . forEach ( entry => {
@@ -127,22 +139,24 @@ <h1 class="text-3xl md:text-4xl font-bold text-gray-900">Resource Timing API Dem
127139 }
128140
129141 // --- Calculate timing metrics ---
130- // All times are in milliseconds. toFixed(2) rounds to 2 decimal places.
131- const dnsTime = ( entry . domainLookupEnd - entry . domainLookupStart ) . toFixed ( 2 ) ;
132- const tcpTime = ( entry . connectEnd - entry . connectStart ) . toFixed ( 2 ) ;
133- // secureConnectionStart will be 0 if the resource is not loaded over HTTPS
134- const tlsTime = entry . secureConnectionStart > 0 ? ( entry . requestStart - entry . secureConnectionStart ) . toFixed ( 2 ) : '0.00' ;
135- // Time to First Byte (TTFB)
136- const requestTime = ( entry . responseStart - entry . requestStart ) . toFixed ( 2 ) ;
137- const downloadTime = ( entry . responseEnd - entry . responseStart ) . toFixed ( 2 ) ;
138- const totalTime = ( entry . duration ) . toFixed ( 2 ) ;
142+ const dnsTime = formatTiming ( entry . domainLookupStart , entry . domainLookupEnd ) ;
143+ const tcpTime = formatTiming ( entry . connectStart , entry . connectEnd ) ;
144+ const tlsTime = formatTiming ( entry . secureConnectionStart , entry . requestStart ) ;
145+ const requestTime = formatTiming ( entry . requestStart , entry . responseStart ) ;
146+ const downloadTime = formatTiming ( entry . responseStart , entry . responseEnd ) ;
147+ const totalTime = formatTiming ( entry . startTime , entry . responseEnd ) ;
148+
139149
140150 // --- Determine if a stage is not applicable (duration is zero) ---
141151 // This can happen if the resource is cached, a connection is reused, or the stage doesn't apply (e.g. TLS for HTTP).
142- const dnsNotApplicable = ( entry . domainLookupStart === undefined ) || ( entry . domainLookupStart === 0.0 ) ;
143- const tcpNotApplicable = ( entry . connectStart === undefined ) || ( entry . connectStart === 0.0 ) ;
144- const tlsNotApplicable = ( entry . secureConnectionStart === undefined ) || ( entry . secureConnectionStart === 0.0 ) ;
145- const requestNotApplicable = ( entry . requestStart === undefined ) || ( entry . requestStart === 0.0 ) ;
152+ // const dnsNotApplicable = (entry.domainLookupStart === undefined) || (entry.domainLookupStart === 0.0);
153+ // const tcpNotApplicable = (entry.connectStart === undefined) || (entry.connectStart === 0.0);
154+ // const tlsNotApplicable = (entry.secureConnectionStart === undefined) || (entry.secureConnectionStart === 0.0);
155+ // const requestNotApplicable = (entry.requestStart === undefined) || (entry.requestStart === 0.0);
156+ const dnsNotApplicable = bothZeroOrUndefined ( entry . domainLookupStart , entry . domainLookupEnd ) ;
157+ const tcpNotApplicable = bothZeroOrUndefined ( entry . connectStart , entry . connectEnd ) ;
158+ const tlsNotApplicable = bothZeroOrUndefined ( entry . secureConnectionStart , entry . secureConnectionEnd ) ;
159+ const requestNotApplicable = bothZeroOrUndefined ( entry . requestStart , entry . requestEnd ) ;
146160
147161 // Use a specific name for the navigation entry, otherwise truncate the URL
148162 const url = entry . entryType === 'navigation'
@@ -196,4 +210,4 @@ <h1 class="text-3xl md:text-4xl font-bold text-gray-900">Resource Timing API Dem
196210 </ script >
197211
198212</ body >
199- </ html >
213+ </ html >
0 commit comments