Skip to content

perf(lib): optimize subscription status lookup to O(n)#848

Merged
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/queries
Apr 12, 2026
Merged

perf(lib): optimize subscription status lookup to O(n)#848
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/queries

Conversation

@ishaanxgupta
Copy link
Copy Markdown
Contributor

Summary

Optimizes the getSubscriptionStatus utility function in packages/lib/queries.ts by converting a quadratic $O(n^2)$ lookup into a linear $O(n)$ map lookup.

Previously, the function iterated over PLAN_TIERS and inside that loop, it called products.find((p) => p.id === tier). This caused redundant full-array scans on the products array for every tier check.

Changes

  • Created a productMap using new Map(products.map((p) => [p.id, p])) before iterating over PLAN_TIERS.
  • Replaced the inner products.find(...) with productMap.get(tier).

Impact

  • CPU Performance: Transforms the algorithmic complexity from $O(n \times m)$ to $O(n + m)$, eliminating redundant array scans during subscription verification checks. This makes subscription status checks slightly faster, especially on pages or operations checking permissions against multiple tiers.

Copy link
Copy Markdown
Member

@MaheshtheDev MaheshtheDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great improvement, thanks for the contritbution

@MaheshtheDev MaheshtheDev enabled auto-merge (squash) April 12, 2026 22:47
Copy link
Copy Markdown
Member

@MaheshtheDev MaheshtheDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great improvement, thanks for the contritbution

@MaheshtheDev MaheshtheDev merged commit e714440 into supermemoryai:main Apr 12, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants