Skip to content

Commit 3e4519d

Browse files
perf: wrap admin-page.tsx filtering logic in useMemo
Co-authored-by: KxlSys <116387953+KxlSys@users.noreply.github.com>
1 parent f8fd2e5 commit 3e4519d

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

.jules/bolt.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
## 2024-07-10 - O(n) Single Pass Data Aggregation
2222
**Learning:** Using chained array methods (e.g. `.map().filter()`, `.flatMap()`, and repeated `.filter()` over the same large list like `profiles`) leads to redundant O(n) passes over data and allocates unnecessary intermediate arrays. This blocks the main thread longer than necessary when calculating basic statistics like `uniqueCities`, `uniqueTechs`, or total counts.
2323
**Action:** Consolidate statistical aggregations over large object arrays into a single O(n) `for` loop that updates multiple accumulators (like `Set` objects and primitive counters) in one pass, thereby improving performance and reducing memory overhead.
24+
## 2024-07-20 - Memoization of filtering logic
25+
**Learning:** In pages with heavily interactive inputs (like `AdminPage` with a search bar), performing O(N) array filtering synchronously within the component body causes the expensive calculation to block every re-render, compounding latency on keystrokes.
26+
**Action:** Wrap any O(N) filtering operations (e.g. `profiles.filter(...)`) in `React.useMemo` to ensure they only re-run when their specific dependencies (`profiles` or `searchQuery`) actually change, completely skipping the overhead on unrelated state updates.

src/pages/admin-page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useState, useMemo } from "react";
22
import { useNavigate, Link } from "react-router-dom";
33
import { Shield, Send, UserPlus, UserMinus, Users, Building2, Handshake, Download, Search, TrendingUp, ArrowUpRight, TriangleAlert, Clock, GitCommitHorizontal, UserRoundPlus, Layers as Layers3, Flag, UserCheck, MapPin, Trash2 } from "lucide-react";
44
import { Button } from "@/components/ui/button";
@@ -171,11 +171,11 @@ export function AdminPage() {
171171
);
172172
}
173173

174-
const filteredProfiles = profiles.filter((p) => {
174+
const filteredProfiles = useMemo(() => profiles.filter((p) => {
175175
if (!searchQuery) return true;
176176
const q = searchQuery.toLowerCase();
177177
return p.full_name.toLowerCase().includes(q) || p.username.toLowerCase().includes(q) || p.city?.toLowerCase().includes(q);
178-
});
178+
}), [profiles, searchQuery]);
179179

180180
// ⚡ Bolt: Consolidate data aggregation into a single O(n) pass
181181
// Avoids multiple redundant iterations and intermediate array allocations

0 commit comments

Comments
 (0)