Skip to content

Commit

Permalink
Merge pull request #37205 from callstack-internal/Audit-Implementatio…
Browse files Browse the repository at this point in the history
…n-memoize-SidebarLinksData

[Audit][Implementation] - Memoize SidebarLinksData
  • Loading branch information
mountiny authored Mar 26, 2024
2 parents 0d152c3 + 2b62829 commit 78e8d27
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/pages/home/sidebar/SidebarLinksData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {deepEqual} from 'fast-equals';
import lodashGet from 'lodash/get';
import lodashMap from 'lodash/map';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import React, {memo, useCallback, useEffect, useMemo, useRef} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
Expand Down Expand Up @@ -136,18 +136,14 @@ function SidebarLinksData({

const reportIDsRef = useRef(null);
const isLoading = isLoadingApp;

const optionItemsMemoized = useMemo(
() => SidebarUtils.getOrderedReportIDs(null, chatReports, betas, policies, priorityMode, allReportActions, transactionViolations, activeWorkspaceID, policyMemberAccountIDs),
[chatReports, betas, policies, priorityMode, allReportActions, transactionViolations, activeWorkspaceID, policyMemberAccountIDs],
);

const optionListItems = useMemo(() => {
const reportIDs = SidebarUtils.getOrderedReportIDs(
null,
chatReports,
betas,
policies,
priorityMode,
allReportActions,
transactionViolations,
activeWorkspaceID,
policyMemberAccountIDs,
);
const reportIDs = optionItemsMemoized;

if (deepEqual(reportIDsRef.current, reportIDs)) {
return reportIDsRef.current;
Expand All @@ -160,7 +156,7 @@ function SidebarLinksData({
reportIDsRef.current = reportIDs;
}
return reportIDsRef.current || [];
}, [chatReports, betas, policies, priorityMode, allReportActions, transactionViolations, activeWorkspaceID, policyMemberAccountIDs, isLoading, network.isOffline, prevPriorityMode]);
}, [optionItemsMemoized, priorityMode, isLoading, network.isOffline, prevPriorityMode]);

// We need to make sure the current report is in the list of reports, but we do not want
// to have to re-generate the list every time the currentReportID changes. To do that
Expand Down Expand Up @@ -334,4 +330,27 @@ export default compose(
initialValue: {},
},
}),
)(SidebarLinksData);
)(
/*
While working on audit on the App Start App metric we noticed that by memoizing SidebarLinksData we can avoid 2 additional run of getOrderedReportIDs.
With that we can reduce app start up time by ~2s on heavy account.
More details - https://github.com/Expensify/App/issues/35234#issuecomment-1926914534
*/
memo(
SidebarLinksData,
(prevProps, nextProps) =>
_.isEqual(prevProps.chatReports, nextProps.chatReports) &&
_.isEqual(prevProps.allReportActions, nextProps.allReportActions) &&
prevProps.isLoadingApp === nextProps.isLoadingApp &&
prevProps.priorityMode === nextProps.priorityMode &&
_.isEqual(prevProps.betas, nextProps.betas) &&
_.isEqual(prevProps.policies, nextProps.policies) &&
prevProps.network.isOffline === nextProps.network.isOffline &&
_.isEqual(prevProps.insets, nextProps.insets) &&
prevProps.onLinkClick === nextProps.onLinkClick &&
_.isEqual(prevProps.policyMembers, nextProps.policyMembers) &&
_.isEqual(prevProps.transactionViolations, nextProps.transactionViolations) &&
_.isEqual(prevProps.currentUserPersonalDetails, nextProps.currentUserPersonalDetails) &&
prevProps.currentReportID === nextProps.currentReportID,
),
);

0 comments on commit 78e8d27

Please sign in to comment.