Skip to content

perf(citations): memoize domain/URL classification by host instead of per citation #366

Closed

Description

@gkhngyk

Summary

The Citations page's server-side aggregation classifies every citation occurrence — calling classifyDomain() (and classifyArticleType()) once per citation — instead of once per distinct domain/URL. On a brand with a lot of history this is the dominant CPU cost of the Citations load: the same host (say reddit.com) that appears in thousands of citations is re-classified thousands of times, and each classifyDomain call scans 8+ suffix lists (.some() over brandDomains, competitorDomains, FORUM_DOMAINS, SOCIAL_DOMAINS, REVIEW_DOMAINS, …) plus regexes.

Memoizing classification by host/URL collapses tens of thousands of calls to a few hundred (the number of distinct domains), with zero behavior change.

Root cause (from the code)

In web/src/lib/actions/citations.ts:

  • getCitationsOverview — nested for (result) { for (cite) { … } } loop calls, per citation:
    • extractHostname(cite.url)
    • classifyDomain(host, classifyCtx)
    • classifyArticleType(cite.url, cite.title)
    • URL normalization (new URL(...))
  • getCitationGaps — same nested loop; classifyDomain(host, classifyCtx) per citation (its domainCat map is re-created per result, so the same host is re-classified across results).

classifyDomain (web/src/lib/citations/classify.ts) runs matchesSuffix (list.some(...)) against 8+ domain lists + a couple of regexes on every call, and classifyArticleType (article-type.ts) tests ~20 regexes per call — so the per-occurrence cost is real.

classifyCtx (brand + competitor domains) is constant for the duration of a single aggregation call, so a domain's classification is stable — memoizing it is safe.

Proposed change

Add per-call memoization keyed by the input, so each distinct value is classified once:

  • A Map<string, SourceCategory> for domain classification — look up by host, compute via classifyDomain(host, classifyCtx) only on a miss.
  • A Map<string, …> for classifyArticleType, keyed by cite.url (or url + '\n' + title).
  • Apply in both getCitationsOverview and getCitationGaps.

Distinct domains/URLs are in the hundreds even when total citations are in the tens of thousands, so this removes the bulk of the classification work. Output is identical (same inputs → same category), so the Domains / URLs / source-type breakdown and the Competitor Gaps results are unchanged.

Explicit non-goals

  • This is a pure in-memory memoization — no DB change, no migration, no behavior change.
  • The separate, larger idea of pushing the citations jsonb unnest + grouping into SQL (so the heavy jsonb isn't transferred and only distinct domains reach JS) is out of scope here — that's a follow-up.

How to test

  1. On a brand with a large citation history, open /dashboard/citations → it loads faster (less server CPU); the Domains, URLs, and source-type breakdowns show the same numbers as before.
  2. The Competitor Gaps tab shows the same gap domains and per-competitor map as before.
  3. A brand with few citations behaves exactly as today.

Notes

  • Server-side (server action) change only; no schema and no UI change.

Activity

  1. added
    enhancement
    good first issue
    help wanted
    on Jul 7, 2026
  2. Sam-syntax35 commented on Jul 7, 2026

    @Sam-syntax35
    Contributor

    i really read all the documentation ! and so far i am enjoying contributing on this project. could you please assign this to me

  3. gkhngyk commented on Jul 7, 2026

    @gkhngyk
    ContributorAuthor

    Thanks @Sam-syntax35, and glad you're enjoying it! You've already got a few issues in flight — let's land a couple of those first and I'll circle back to assign this one. It's a perf-sensitive change (the output has to stay identical), so it benefits from a bit more room anyway. Appreciate the enthusiasm! 🙌

  4. d180 commented on Jul 7, 2026

    @d180
    Contributor

    Hi @gkhngyk, looked into this. The issue is classifyDomain and classifyArticleType being called on every citation in the loop even when the same host repeats thousands of times. Since both are pure functions and classifyCtx is constant per call, a simple Map cache before the loop would collapse tens of thousands of calls to a few hundred with zero behavior change. Would love to take this one, let me know if the approach sounds good!

  5. gkhngyk commented on Jul 9, 2026

    @gkhngyk
    ContributorAuthor

    Closing as already resolved. 🎉

    This landed in #375, which added host-level memoization to web/src/lib/actions/citations.ts:

    • domainClassificationCache = new Map<string, SourceCategory>() — domain classification is now computed once per host instead of per citation
    • articleTypeCache = new Map<string, ArticleType | null>() — article-type classification is memoized the same way

    Both caches are wired through the overview and gaps aggregation, so repeated hosts/URLs no longer re-run classifyDomain / classifyArticleType. #375 didn't reference this issue in its description, so it stayed open — but the work is on main. Nothing left to do here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions