Skip to content

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

Open
ishaanxgupta wants to merge 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/queries
Open

perf(lib): optimize subscription status lookup to O(n)#848
ishaanxgupta wants to merge 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.

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.

1 participant