@@ -37,21 +37,20 @@ export function getXhrPerfomanceData(
3737 const possibleEntries = getPossiblePerfResourceEntries (
3838 filteredSortedPerfEntries
3939 ) ;
40- const bestEntry = getBestPerfResourceTiming ( possibleEntries , span ) ;
41- return bestEntry ;
40+ return getBestPerfResourceTiming ( possibleEntries , span ) ;
4241}
4342
4443/**
4544 * First step for the algorithm. Filter the Performance Resource Timings by the
4645 * name (it should match the XHR URL), additionally, the start/end timings of
4746 * every performance entry should fit within the span start/end timings. Also,
48- * the entry should be already assigned to a span.
47+ * the entry should not be already assigned to a span.
4948 * These filtered performance resource entries are considered as possible
5049 * entries associated to the xhr.
5150 * Those are possible because there might be more than two entries that pass the
5251 * filter.
5352 * Additionally, the returned array is sorted by the entries' `startTime` as
54- * getEntriesByType() already does it.
53+ * getEntriesByType() already does it (https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType#Return_Value) .
5554 * @param xhrUrl
5655 * @param span
5756 */
@@ -71,9 +70,8 @@ export function getPerfSortedResourceEntries(
7170 * As the XHR could cause a CORS pre-flight request, we have to look for
7271 * possible performance entries either containing cors preflight timings or not.
7372 * A possible entry with cors data is when a resource timing entry does not
74- * overlap timings with other resource timing entry. Also, a possible entry
75- * without cors resource timing is when that resource timing entry is not
76- * 'paired' with any other entry.
73+ * overlap timings with other resource timing entry. Also, every entry is
74+ * considered as possible XHR performance entry.
7775 * Thus, for this step traverse the array of resource entries and for every
7876 * entry check if it is a possible performance resource entry.
7977 * @param filteredSortedPerfEntries Sorted array of Performance Resource
@@ -83,9 +81,10 @@ export function getPossiblePerfResourceEntries(
8381 filteredSortedPerfEntries : PerformanceResourceTiming [ ]
8482) : XhrPerformanceResourceTiming [ ] {
8583 const possiblePerfEntries = new Array < XhrPerformanceResourceTiming > ( ) ;
86- // As this part of the algorithm traverses the array twice, although,
87- // this array is not large as the performance resource entries buffer is
88- // cleared when there are no more running XHRs.
84+ // As this part of the algorithm uses nested for loops to examine pairs of
85+ // entries, although, this array is not large as the performance resource
86+ // entries buffer is cleared when there are no more running XHRs. Also, this
87+ // data is already filtered by URL and start/end time to be within the span.
8988 for ( let i = 0 ; i < filteredSortedPerfEntries . length ; i ++ ) {
9089 const entryI = filteredSortedPerfEntries [ i ] ;
9190 // Consider the current entry as a possible entry without cors preflight
@@ -97,7 +96,7 @@ export function getPossiblePerfResourceEntries(
9796 // way we to avoid comparing twice the entries and taking the wrong order.
9897 for ( let j = i + 1 ; j < filteredSortedPerfEntries . length ; j ++ ) {
9998 const entryJ = filteredSortedPerfEntries [ j ] ;
100- if ( ! isPossibleCorsPair ( entryI , entryJ ) ) {
99+ if ( isPossibleCorsPair ( entryI , entryJ ) ) {
101100 // As the entries are not overlapping, that means those timings
102101 // are possible perfomance timings related to the XHR.
103102 possiblePerfEntries . push ( {
@@ -110,17 +109,22 @@ export function getPossiblePerfResourceEntries(
110109 return possiblePerfEntries ;
111110}
112111
113- // Pick the best performance resource timing for the XHR: Using the possible
114- // performance resource timing entries from previous step, the best entry will
115- // be the one with the minimum gap to the span start/end timings.
116- // The performance resource timing entry with the minimum gap to the span
117- // start/end timings points out that entry is the best fit for the span.
112+ /**
113+ * Pick the best performance resource timing for the XHR: Using the possible
114+ * performance resource timing entries from previous step, the best entry will
115+ * be the one with the minimum gap to the span start/end timings.
116+ * The performance resource timing entry with the minimum gap to the span
117+ * start/end timings points out that entry is the best fit for the span.
118+ *
119+ * @param perfEntries
120+ * @param span
121+ */
118122function getBestPerfResourceTiming (
119123 perfEntries : XhrPerformanceResourceTiming [ ] ,
120124 span : Span
121125) : XhrPerformanceResourceTiming | undefined {
122126 let minimumGapToSpan = Number . MAX_VALUE ;
123- let bestPerfEntry : XhrPerformanceResourceTiming | undefined = undefined ;
127+ let bestPerfEntry : XhrPerformanceResourceTiming | undefined ;
124128 for ( const perfEntry of perfEntries ) {
125129 // If the current entry has cors preflight data use its `startTime` to
126130 // calculate the gap to the span.
@@ -142,6 +146,11 @@ function getBestPerfResourceTiming(
142146 return bestPerfEntry ;
143147}
144148
149+ /**
150+ * A Performance entry is part of a XHR if entry has not been assigned
151+ * previously to another XHR and the URL is the same as the XHR and the
152+ * entry's start/end times are within the span's start/end times.
153+ */
145154function isPerfEntryPartOfXhr (
146155 entry : PerformanceResourceTiming ,
147156 xhrUrl : string ,
@@ -155,12 +164,20 @@ function isPerfEntryPartOfXhr(
155164 ) ;
156165}
157166
167+ /**
168+ * A possible CORS pair is defined when the entries does not overlap in their
169+ * start/end times.
170+ */
158171function isPossibleCorsPair (
159172 entry1 : PerformanceResourceTiming ,
160173 entry2 : PerformanceResourceTiming
161174) : boolean {
175+ // To determine if the entries overlap, the minimum responseEnd should be
176+ // less than the maximum startTime (e.g. entry1 with startTime = 1 and
177+ // responseEnd = 3 and entry2 with startTime = 2 and responseEnd = 4, the
178+ // minimum is 3 and the maximum is 2, this tells that the intervals overlap).
162179 return (
163- Math . min ( entry1 . responseEnd , entry2 . responseEnd ) >=
180+ Math . min ( entry1 . responseEnd , entry2 . responseEnd ) <
164181 Math . max ( entry1 . startTime , entry2 . startTime )
165182 ) ;
166183}
0 commit comments