From 82929104c16b29a39e35a1582b695c66a2449fe0 Mon Sep 17 00:00:00 2001 From: therealsama <263038098+therealsama@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:23:59 +0200 Subject: [PATCH] perf: load comments in one search, share thread downloads, speed up swipes A big thread's comments took up to 15 s to load. Algolia was paged 200 hits at a time, and Firebase reads went in batches of 10, a leftover from when items were read over REST. Reads now share one WebSocket, so batches only add round trips. - One Algolia search per thread, for up to 1000 hits, its cap. It asks for the four fields a comment needs and no highlighting, which makes the response 70% smaller. An empty attributesToHighlight is ignored, so it takes an empty list. - Firebase reads go out all at once: for ordering, missing parents and reply checks. - A prefetch orders the whole thread, not just its top level, so the cache's orderedDepth and the hook's ordering completion are gone. - With no copy of the item, its comments load alongside it, from the same Firebase read. - A thread with more than 1000 comments still merges Algolia's item tree. A Firebase count over 1000 now starts it alongside the search, and it is cancelled when search turns out to have every comment. - A comment page drops a story's comments when there are more than 1000, so a count of them, under 1 KB, now cancels that search. On mobile, the swipe viewer prefetches each other feed's list and first thread so a feed switch shows at once. A story often heads two feeds, so its thread was downloaded by the feed prefetch, the next-items prefetch and the story on screen at the same time. - Callers that want the same thread share one download. An abort only detaches its caller, and the download stops once all have. - Lists go out together, not one feed after another 500 ms apart, and threads follow one at a time as their lists arrive, so a stalled list holds up nothing else. - It prefetches the story a feed opens on, skipping ones read in an earlier session, and only a thread with no copy: a stale one shows at once and refreshes when opened. - It starts once the current feed's stories are on screen, including when a stale cache shows during a stalled refresh. A 772-comment thread loads in 1.3 s (was 4.9 s), and a 3,378-comment one in under 4.2 s (was 15.1 s). On slow 4G, switching to a feed whose thread is still prefetching takes 8.7 s (was 26.1 s). Opening a story from the list, by its comments or a text post's title, cancelled every prefetch, its own too, so its page downloaded the thread again. That one now keeps running and the page joins it. On desktop, a 200-comment story opened mid-prefetch shows its comments in 468 ms (was 877 ms). Each swipe re-rendered every comment in the five mounted panels, and each render did more work than it needed to. - Comment is memoized. - A comment's timestamp tooltip reuses one Intl.DateTimeFormat, where toLocaleString built a new one per call. - index.html has a with no href. React Router's hash history looks one up for every link, which scanned the whole page when there was none. - A comment is sanitized once, not twice, unless auto-linking added a link. On Pixel 7 emulation at 4x CPU, 8 swipes take 37-45% less main-thread time, and opening a 3,815-comment thread cuts its longest task from 525 ms to 425 ms. --- index.html | 4 + src/api/hn.test.ts | 202 +++++++++++++++-- src/api/hn.ts | 290 ++++++++++++++----------- src/components/Comment.tsx | 7 +- src/components/FullScreenItem.tsx | 1 - src/components/StoryCard.test.tsx | 55 ++++- src/components/StoryCard.tsx | 12 +- src/components/SwipeStoryViewer.tsx | 2 +- src/hooks/useItemWithComments.test.tsx | 158 +++++--------- src/hooks/useItemWithComments.ts | 57 ++--- src/hooks/usePrefetchItem.test.ts | 2 +- src/hooks/usePrefetchItem.ts | 16 +- src/hooks/usePrefetchSections.test.ts | 109 ++++++++++ src/hooks/usePrefetchSections.ts | 145 ++++++------- src/types.ts | 1 - src/utils/itemCache.test.ts | 36 --- src/utils/itemCache.ts | 12 +- src/utils/sanitize.ts | 5 +- 18 files changed, 680 insertions(+), 434 deletions(-) create mode 100644 src/hooks/usePrefetchSections.test.ts diff --git a/index.html b/index.html index d39e1c7..fa2efbb 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,10 @@ + +