Skip to content

Build index Maps for repeated .find() lookups in chat and document hooks #1047

@MODSetter

Description

@MODSetter

Description

Several hot paths use .find() inside .map(), creating O(n*m) lookups that scale with chat length and team/document count.

Vercel React Best Practices Rule: js-index-maps (7.2)

Files to change

1. surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx (lines ~229-235)

return syncedMessages.map((msg) => {
  const member = msg.author_id ? membersData?.find((m) => m.user_id === msg.author_id) : null;
  const existingMsg = prev.find((m) => m.id === `msg-${msg.id}`);

Fix: Build Maps once before the loop:

const memberById = new Map(membersData?.map(m => [m.user_id, m]) ?? []);
const prevById = new Map(prev.map(m => [m.id, m]));

return syncedMessages.map((msg) => {
  const member = msg.author_id ? memberById.get(msg.author_id) : null;
  const existingMsg = prevById.get(`msg-${msg.id}`);

2. surfsense_web/hooks/use-documents.ts (lines ~249-251)

const liveItem = validItems.find((v) => v.id === existing.id);

Fix:

const liveById = new Map(validItems.map(v => [v.id, v]));
// Then: liveById.get(existing.id)

3. surfsense_web/hooks/use-inbox.ts (lines ~160-161)

const liveItem = recentItems.find((v) => v.id === existing.id);

Fix: Same Map pattern.

Acceptance criteria

  • No .find() inside .map() loops for the listed patterns
  • All lookups use pre-built Map with .get()
  • Chat messages, documents, and inbox items still display correctly

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions