Skip to content

Commit b2a66ab

Browse files
perf: optimize category and profile calculation in CategoryFilterWidget
Combine the calculation of activeCategories and uniqueProfiles into a single useMemo hook that iterates over the shortcuts array once using standard for loops. This replaces multiple mapping, flatMapping, and spreading operations which are computationally expensive and trigger garbage collection unnecessarily. Co-authored-by: alazndy <78882672+alazndy@users.noreply.github.com>
1 parent 7593f90 commit b2a66ab

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

components/CategoryFilterWidget.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ import { UsersIcon } from '@heroicons/react/24/outline';
66
export const CategoryFilterWidget: React.FC = () => {
77
const { shortcuts, filterCategory, setFilterCategory, filterProfile, setFilterProfile } = useGTab();
88

9-
const activeCategories = useMemo(() => {
10-
return ['All', ...new Set(shortcuts.map(s => s.category))];
11-
}, [shortcuts]);
9+
const { activeCategories, uniqueProfiles } = useMemo(() => {
10+
const categorySet = new Set<string>();
11+
const profileSet = new Set<string>();
12+
13+
for (const s of shortcuts) {
14+
if (s.category) categorySet.add(s.category);
15+
if (s.profiles) {
16+
for (const p of s.profiles) {
17+
if (p.name) profileSet.add(p.name);
18+
}
19+
}
20+
}
1221

13-
const uniqueProfiles = useMemo(() => {
14-
return Array.from(new Set(shortcuts.flatMap(s => s.profiles?.map(p => p.name) || []))).sort();
22+
return {
23+
activeCategories: ['All', ...categorySet],
24+
uniqueProfiles: Array.from(profileSet).sort()
25+
};
1526
}, [shortcuts]);
1627

1728
return (

0 commit comments

Comments
 (0)