Skip to content

Commit b9d902d

Browse files
Replace indexOf with precomputed index map in renderItem
Avoids O(n) array scan per rendered row by precomputing a reportActionID-to-index Map once per data change. Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
1 parent f20ecda commit b9d902d

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/pages/inbox/report/ReportActionsList.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,16 @@ function ReportActionsList({
695695
return isExpenseReport(report) || isIOUReport(report) || isInvoiceReport(report);
696696
}, [parentReportAction, report, sortedVisibleReportActions]);
697697

698+
// Precompute a reportActionID → index map so renderItem can resolve the real index in O(1)
699+
// instead of scanning sortedVisibleReportActions with indexOf on every render.
700+
const actionIndexMap = useMemo(() => {
701+
const map = new Map<string, number>();
702+
for (const [i, action] of sortedVisibleReportActions.entries()) {
703+
map.set(action.reportActionID, i);
704+
}
705+
return map;
706+
}, [sortedVisibleReportActions]);
707+
698708
const renderItem = useCallback(
699709
({item: reportAction, index}: ListRenderItemInfo<OnyxTypes.ReportAction>) => {
700710
const originalReportID = getOriginalReportID(report.reportID, reportAction, reportActionsFromOnyx);
@@ -703,8 +713,7 @@ function ReportActionsList({
703713
// Use the action's actual index in sortedVisibleReportActions rather than the FlashList-provided index,
704714
// because useFlashListScrollKey may slice the data for deep-link scroll positioning, making the
705715
// FlashList index offset from the full array and causing wrong displayAsGroup computation.
706-
const actionIndex = sortedVisibleReportActions.indexOf(reportAction);
707-
const safeIndex = actionIndex >= 0 ? actionIndex : index;
716+
const safeIndex = actionIndexMap.get(reportAction.reportActionID) ?? index;
708717

709718
return (
710719
<>
@@ -760,6 +769,7 @@ function ReportActionsList({
760769
isOffline,
761770
transactionThreadReport,
762771
linkedReportActionID,
772+
actionIndexMap,
763773
sortedVisibleReportActions,
764774
shouldHideThreadDividerLine,
765775
unreadMarkerReportActionID,

0 commit comments

Comments
 (0)