Skip to content

Commit f2db94f

Browse files
authored
Merge pull request Expensify#65727 from callstack-internal/perf/improve-tab-switching
perf: use Set instead of includes in useEffect on Search page
2 parents a9fb76a + 5d913f2 commit f2db94f

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/hooks/useSearchHighlightAndScroll.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
6161
return;
6262
}
6363

64-
const hasTransactionsIDsChange = transactionsIDs.some((id) => !previousTransactionsIDs.includes(id));
65-
const hasReportActionsIDsChange = reportActionsIDs.some((id) => !previousReportActionsIDs.includes(id));
64+
const previousTransactionsIDsSet = new Set(previousTransactionsIDs);
65+
const previousReportActionsIDsSet = new Set(previousReportActionsIDs);
66+
const hasTransactionsIDsChange = transactionsIDs.some((id) => !previousTransactionsIDsSet.has(id));
67+
const hasReportActionsIDsChange = reportActionsIDs.some((id) => !previousReportActionsIDsSet.has(id));
6668

6769
// Check if there is a change in the transactions or report actions list
6870
if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange || hasPendingSearchRef.current) {
@@ -74,12 +76,14 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
7476
hasPendingSearchRef.current = false;
7577

7678
const newIDs = isChat ? reportActionsIDs : transactionsIDs;
77-
const hasAGenuinelyNewID = newIDs.some((id) => !existingSearchResultIDs.includes(id));
79+
const existingSearchResultIDsSet = new Set(existingSearchResultIDs);
80+
const hasAGenuinelyNewID = newIDs.some((id) => !existingSearchResultIDsSet.has(id));
7881

7982
// Only skip search if there are no new items AND search results aren't empty
8083
// This ensures deletions that result in empty data still trigger search
8184
if (!hasAGenuinelyNewID && existingSearchResultIDs.length > 0) {
82-
const hasDeletedID = existingSearchResultIDs.some((id) => !newIDs.includes(id));
85+
const newIDsSet = new Set(newIDs);
86+
const hasDeletedID = existingSearchResultIDs.some((id) => !newIDsSet.has(id));
8387
if (!hasDeletedID) {
8488
return;
8589
}

0 commit comments

Comments
 (0)