From 8837e48185450b42e6d1cec06fc766528e859ef0 Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Thu, 29 Oct 2026 01:42:40 +0330 Subject: [PATCH 1/2] perf(virtualized-lists): use a Set for stale-index lookup in ViewabilityHelper _onUpdateSync filtered the indices to check with this._viewableIndices.includes(ii), which is O(n) per index and O(n^2) per update. Build a Set of the current viewable indices once and use has() instead. Behavior is unchanged. --- packages/virtualized-lists/Lists/ViewabilityHelper.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/virtualized-lists/Lists/ViewabilityHelper.js b/packages/virtualized-lists/Lists/ViewabilityHelper.js index 5fc5addc11b8..0a3841b0e8b5 100644 --- a/packages/virtualized-lists/Lists/ViewabilityHelper.js +++ b/packages/virtualized-lists/Lists/ViewabilityHelper.js @@ -302,8 +302,10 @@ class ViewabilityHelper { ) => ViewToken, ) { // Filter out indices that have gone out of view since this call was scheduled. + // Use a Set so the lookup is O(1) per index instead of O(n) (`includes`). + const currentViewableIndices = new Set(this._viewableIndices); viewableIndicesToCheck = viewableIndicesToCheck.filter(ii => - this._viewableIndices.includes(ii), + currentViewableIndices.has(ii), ); const prevItems = this._viewableItems; const nextItems = new Map( From 7f62d7c0956bc0248ec30e22e15497d322bd9677 Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Thu, 29 Oct 2026 21:58:07 +0330 Subject: [PATCH 2/2] perf(virtualized-lists): skip no-op stale-index filter in ViewabilityHelper._onUpdateSync Both call sites pass the array stored in this._viewableIndices, so the filter always keeps every index. Skip it when the input is the stored array and keep the filter for any other caller. Drops the Set; behavior is unchanged. --- .../virtualized-lists/Lists/ViewabilityHelper.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/virtualized-lists/Lists/ViewabilityHelper.js b/packages/virtualized-lists/Lists/ViewabilityHelper.js index 0a3841b0e8b5..150d07363b24 100644 --- a/packages/virtualized-lists/Lists/ViewabilityHelper.js +++ b/packages/virtualized-lists/Lists/ViewabilityHelper.js @@ -302,11 +302,14 @@ class ViewabilityHelper { ) => ViewToken, ) { // Filter out indices that have gone out of view since this call was scheduled. - // Use a Set so the lookup is O(1) per index instead of O(n) (`includes`). - const currentViewableIndices = new Set(this._viewableIndices); - viewableIndicesToCheck = viewableIndicesToCheck.filter(ii => - currentViewableIndices.has(ii), - ); + // Both call sites pass the array stored in `this._viewableIndices`, in which + // case every index is trivially still viewable and the O(n^2) filter can be + // skipped. + if (viewableIndicesToCheck !== this._viewableIndices) { + viewableIndicesToCheck = viewableIndicesToCheck.filter(ii => + this._viewableIndices.includes(ii), + ); + } const prevItems = this._viewableItems; const nextItems = new Map( viewableIndicesToCheck.map(ii => {