Files
Codewalkers/apps/web/src/components/review/comment-index.ts
Lukas May ec86b62d8d perf: Pre-index review comments into Map to eliminate O(n) filtering
Build a Map<string, ReviewComment[]> once in ReviewTab and thread it
down through DiffViewer → FileCard → HunkRows, replacing O(n) filter
calls with O(1) map lookups. With 200 comments and 5000 diff lines,
this reduces ~1M iterations per render cycle to ~5K.

Key: "${filePath}:${lineNumber}:${lineType}" for line-level comments,
"${filePath}:file" for file-level (lineNumber === null) comments.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 19:31:57 +01:00

26 lines
910 B
TypeScript

import type { ReviewComment } from "./types";
/**
* Build a Map keyed by `"${filePath}:${lineNumber}:${lineType}"` for line-level
* comments, or `"${filePath}:file"` for file-level comments (lineNumber === null).
*
* The compound key (filePath + lineNumber + lineType) is required because
* added and removed lines can share the same numeric position in a replacement
* hunk (e.g., old line 10 removed, new line 10 added).
*/
export function buildCommentIndex(
comments: ReviewComment[],
): Map<string, ReviewComment[]> {
const map = new Map<string, ReviewComment[]>();
for (const comment of comments) {
const key =
comment.lineNumber != null
? `${comment.filePath}:${comment.lineNumber}:${comment.lineType}`
: `${comment.filePath}:file`;
const existing = map.get(key);
if (existing) existing.push(comment);
else map.set(key, [comment]);
}
return map;
}