@@ -63,7 +63,7 @@ let pendingLoAFs: PerformanceLongAnimationFrameTiming[] = [];
63
63
let pendingEntriesGroups : pendingEntriesGroup [ ] = [ ] ;
64
64
65
65
// The `processingEnd` time of most recently-processed event, chronologically.
66
- let latestProcessingEnd : number ;
66
+ let latestProcessingEnd : number = 0 ;
67
67
68
68
// A WeakMap to look up the event-timing-entries group of a given entry.
69
69
// Note that this only maps from "important" entries: either the first input or
@@ -188,7 +188,7 @@ const cleanupEntries = () => {
188
188
189
189
// Keep all pending LoAF entries that either:
190
190
// 1) intersect with entries in the newly cleaned up `pendingEntriesGroups`
191
- // 2) occur after the most recently-processed event entry.
191
+ // 2) occur after the most recently-processed event entry (for up to MAX_PREVIOUS_FRAMES)
192
192
const loafsToKeep : Set < PerformanceLongAnimationFrameTiming > = new Set ( ) ;
193
193
for ( let i = 0 ; i < pendingEntriesGroups . length ; i ++ ) {
194
194
const group = pendingEntriesGroups [ i ] ;
@@ -198,18 +198,15 @@ const cleanupEntries = () => {
198
198
} ,
199
199
) ;
200
200
}
201
- for ( let i = 0 ; i < MAX_PREVIOUS_FRAMES ; i ++ ) {
202
- // Look at pending LoAF in reverse order so the most recent are first.
203
- const loaf = pendingLoAFs [ pendingLoAFs . length - 1 - i ] ;
204
-
205
- // If we reach LoAFs that overlap with event processing,
206
- // we can assume all previous ones have already been handled.
207
- if ( ! loaf || loaf . startTime < latestProcessingEnd ) break ;
208
-
209
- loafsToKeep . add ( loaf ) ;
210
- }
201
+ const prevFrameIndexCutoff = pendingLoAFs . length - 1 - MAX_PREVIOUS_FRAMES ;
202
+ // Filter `pendingLoAFs` to preserve LoAF order.
203
+ pendingLoAFs = pendingLoAFs . filter ( ( loaf , index ) => {
204
+ if ( loaf . startTime > latestProcessingEnd && index > prevFrameIndexCutoff ) {
205
+ return true ;
206
+ }
211
207
212
- pendingLoAFs = Array . from ( loafsToKeep ) ;
208
+ return loafsToKeep . has ( loaf ) ;
209
+ } ) ;
213
210
214
211
// Reset the idle callback handle so it can be queued again.
215
212
idleHandle = - 1 ;
0 commit comments