Skip to content

Conversation

@shkumbinhasani
Copy link
Contributor

Summary

  • Fixes model dialog lag that occurs during longer sessions
  • Replaces O(n) array lookups with O(1) Set/Map lookups in the visible() function

Problem

The visible() function in packages/app/src/context/local.tsx was called inside a .filter() loop for each model. Each call performed:

  1. A linear search through store.user array
  2. A linear search through latest() array (which involves expensive date computations)

With many models, this became O(n²) and caused noticeable lag when opening the model selector.

Solution

Added two memoized lookup structures:

  • latestSet: A Set for O(1) lookup of whether a model is "latest"
  • userVisibilityMap: A Map for O(1) lookup of user visibility preferences

The memos are only recomputed when their underlying data changes, not on every visible() call.

The visible() function was called in a filter loop for each model,
performing O(n) lookups on both store.user and latest() arrays.
This caused noticeable lag in the model dialog during longer sessions.

Replace linear array searches with memoized Set and Map structures:
- latestSet: O(1) lookup for whether a model is in the latest set
- userVisibilityMap: O(1) lookup for user visibility preferences

This reduces the overall complexity from O(n²) to O(n) when filtering
the model list.
@adamdotdevin adamdotdevin merged commit e93699b into anomalyco:dev Jan 5, 2026
2 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.

2 participants