Skip to content

perf: Optimize array traversal for empty memory-graph documents#770

Merged
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ved015:perf/memory
Mar 8, 2026
Merged

perf: Optimize array traversal for empty memory-graph documents#770
MaheshtheDev merged 1 commit intosupermemoryai:mainfrom
ved015:perf/memory

Conversation

@ved015
Copy link
Contributor

@ved015 ved015 commented Mar 6, 2026

What:

Replaced nodes.filter((n) => n.type === "document").length === 0 with !nodes.some((n) => n.type === "document") across all memory-graph.tsx components (apps/web/, packages/ui/, and packages/memory-graph/).

Why:

The previous approach (filter().length === 0) was highly inefficient because it would:

  1. Traverse the entire array of nodes no matter what.
  2. Allocate memory for a brand-new filtered array.
  3. Finally check the .length of the newly created array.

Using !nodes.some(...) stops the iteration immediately once a match is found and completely avoids the array allocation, resulting in better performance. I tested it on a benchmark by comparing both approaches using an array of 1,000,000 nodes running 100 iterations.

  1. Best Case (First node is a document):
    • Baseline (filter) 700ms
    • Optimized (some) 0.2ms (~3684x improvement)
  2. Typical Case (1% documents distributed randomly):
    • Baseline (filter): 903.60ms
    • Optimized (some): 0.46ms (~1964x improvement)

@Dhravya @MaheshtheDev please have a look

@graphite-app graphite-app bot requested a review from Dhravya March 6, 2026 17:26
@ved015
Copy link
Contributor Author

ved015 commented Mar 7, 2026

hi @MaheshtheDev can we merge this now

@MaheshtheDev MaheshtheDev merged commit 9f7f415 into supermemoryai:main Mar 8, 2026
1 check 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