Skip to content

Commit 09ea2de

Browse files
committed
perf: deduplicate inbox search results in linear time
1 parent 53f4657 commit 09ea2de

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎resources/js/lib/conversation-search.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export function conversationsMatching(
1515
const isPersonMatch = ({ handle, name }: User) =>
1616
isMatch(handle) || isMatch(name);
1717

18+
const seen = new Set<string>();
19+
1820
return [
1921
...found,
2022
...onHand.filter(
@@ -26,8 +28,12 @@ export function conversationsMatching(
2628
isPersonMatch(user),
2729
),
2830
),
29-
].filter(
30-
(conversation, index, all) =>
31-
all.findIndex(({ id }) => id === conversation.id) === index,
32-
);
31+
].filter(({ id }) => {
32+
if (seen.has(id)) {
33+
return false;
34+
}
35+
36+
seen.add(id);
37+
return true;
38+
});
3339
}

‎tests/Unit/ConversationSearch.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ test('keeps every conversation the server found for the term, ahead of stale cop
3939

4040
assert.deepEqual(conversationsMatching('review', [org('core', 'Core', [ben])], found), [...found]);
4141
});
42+
43+
test('deduplicates many matching server and local copies without changing their first match', () => {
44+
const first = direct(ana, { ahead: 4 });
45+
const duplicates = Array.from({ length: 100 }, () => direct(ana));
46+
const last = org('team', 'Ana team', [ben]);
47+
48+
assert.deepEqual(
49+
conversationsMatching('ana', [...duplicates, last], [first, first]),
50+
[first, last],
51+
);
52+
});

0 commit comments

Comments
 (0)