Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

perf(dashboard): fold the chat search query once per search

Yudhi Armyndharis002a5a2ed0e6
diff --git a/dashboard/src/utils/chatFilters.test.ts b/dashboard/src/utils/chatFilters.test.ts
index 816149ee9..03f09ec49 100644
--- a/dashboard/src/utils/chatFilters.test.ts
+++ b/dashboard/src/utils/chatFilters.test.ts
@@ -212,6 +212,45 @@ for (const [script, zero] of [
});
}
+// The filters run over every row on every render of the Chats page. Folding the query once per row made a search in
+// native digits about nine times the cost of an ASCII one (13 ms against 1.3 ms per filter of 5000 chats).
+test('the query is folded once per search, not once per row', t => {
+ const rows = (count: number) => Array.from({ length: count }, (_, i) => chat(`62812${i}@c.us`, `Contact ${i}`));
+ const replace = t.mock.method(String.prototype, 'replace');
+ const folds = (count: number, query: string): number => {
+ replace.mock.resetCalls();
+ filterChats(rows(count), query);
+ filterChannels(
+ rows(count).map(c => ({ id: c.id, name: c.name! })),
+ query,
+ );
+ groupStatusesByContact(
+ rows(count).map(c => ({ contact: c, timestamp: '2026-01-01T00:00:00Z' })),
+ query,
+ );
+ return replace.mock.callCount();
+ };
+ assert.equal(folds(1000, native('6281', 0x0660)), folds(10, native('6282', 0x0660)));
+});
+
+// A fold kept from one query must never answer for the next, as the operator types and deletes.
+test('each query matches its own rows when the search changes back and forth', () => {
+ const chats = [chat('[email protected]', 'Ben'), chat('[email protected]', 'Anna')];
+ for (const [query, expected] of [
+ ['ben', ['Ben']],
+ [native('1555', 0x0660), ['Anna']],
+ [native('6281', 0x06f0), ['Ben']],
+ ['ann', ['Anna']],
+ ['ben', ['Ben']],
+ ] as const) {
+ assert.deepEqual(
+ filterChats(chats, query).map(c => c.name),
+ expected,
+ query,
+ );
+ }
+});
+
test('a name typed with letters still matches as typed', () => {
assert.deepEqual(
filterChats([chat('[email protected]', 'Ben'), chat('[email protected]', 'Anna')], 'ANN').map(c => c.id),
diff --git a/dashboard/src/utils/chatFilters.ts b/dashboard/src/utils/chatFilters.ts
index 8b681b6ed..907a70178 100644
--- a/dashboard/src/utils/chatFilters.ts
+++ b/dashboard/src/utils/chatFilters.ts
@@ -8,11 +8,18 @@
*/
import { normalizePhoneText } from './phone.ts';
+// The last query as matches() reads it. Every filter calls matches() once per row on every render of the Chats page,
+// and folding a native digit walks back to its script's zero, so the fold is made once per query, not once per row.
+let lastQuery = { query: '', needle: '', folded: '' };
+
/** Case-insensitive "does any of these fields contain the query". Absent fields never match. */
export const matches = (query: string, ...fields: (string | undefined)[]): boolean => {
- const needle = query.toLowerCase();
- // A number typed in the digits of another script matches the ASCII digits of an id.
- const folded = normalizePhoneText(needle);
+ if (query !== lastQuery.query) {
+ const needle = query.toLowerCase();
+ // A number typed in the digits of another script matches the ASCII digits of an id.
+ lastQuery = { query, needle, folded: normalizePhoneText(needle) };
+ }
+ const { needle, folded } = lastQuery;
return fields.some(f => {
const field = (f ?? '').toLowerCase();
return field.includes(needle) || (folded !== needle && field.includes(folded));