From da3086853c4bfb6573f61f0f9657bc66a73bb501 Mon Sep 17 00:00:00 2001 From: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Date: Wed, 4 Nov 2026 02:35:24 +0530 Subject: [PATCH] perf: deduplicate inbox search results in linear time --- resources/js/lib/conversation-search.ts | 14 ++++++++++---- tests/Unit/ConversationSearch.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/resources/js/lib/conversation-search.ts b/resources/js/lib/conversation-search.ts index 225e1d573..d76fe18c1 100644 --- a/resources/js/lib/conversation-search.ts +++ b/resources/js/lib/conversation-search.ts @@ -15,6 +15,8 @@ export function conversationsMatching( const isPersonMatch = ({ handle, name }: User) => isMatch(handle) || isMatch(name); + const seen = new Set(); + return [ ...found, ...onHand.filter( @@ -26,8 +28,12 @@ export function conversationsMatching( isPersonMatch(user), ), ), - ].filter( - (conversation, index, all) => - all.findIndex(({ id }) => id === conversation.id) === index, - ); + ].filter(({ id }) => { + if (seen.has(id)) { + return false; + } + + seen.add(id); + return true; + }); } diff --git a/tests/Unit/ConversationSearch.test.ts b/tests/Unit/ConversationSearch.test.ts index 021020213..b16161cb2 100644 --- a/tests/Unit/ConversationSearch.test.ts +++ b/tests/Unit/ConversationSearch.test.ts @@ -39,3 +39,14 @@ test('keeps every conversation the server found for the term, ahead of stale cop assert.deepEqual(conversationsMatching('review', [org('core', 'Core', [ben])], found), [...found]); }); + +test('deduplicates many matching server and local copies without changing their first match', () => { + const first = direct(ana, { ahead: 4 }); + const duplicates = Array.from({ length: 100 }, () => direct(ana)); + const last = org('team', 'Ana team', [ben]); + + assert.deepEqual( + conversationsMatching('ana', [...duplicates, last], [first, first]), + [first, last], + ); +});