Skip to content

perf(client): make getTrades pagination O(N) instead of O(N^2)#40

Open
KENILSHAHH wants to merge 1 commit intoPolymarket:mainfrom
KENILSHAHH:perf/get-trades-pagination
Open

perf(client): make getTrades pagination O(N) instead of O(N^2)#40
KENILSHAHH wants to merge 1 commit intoPolymarket:mainfrom
KENILSHAHH:perf/get-trades-pagination

Conversation

@KENILSHAHH
Copy link
Copy Markdown

@KENILSHAHH KENILSHAHH commented Apr 14, 2026

Summary

  • ClobClient.getTrades accumulated paginated results with results = [...results, ...response.data] inside the while loop, re-allocating and copying the entire accumulator on
    every page — O(N²) total work in trade count, plus heavy GC churn.
  • Switched to in-place results.push(trade) appends. Same semantics, now O(N).
  • Added a guard for empty/undefined response.data pages (defensive; matches original behavior).

Context

For accounts with large trade histories (e.g. 50 pages × 500 trades = 25k), the old code
performed ~1275× more element copies than necessary and allocated a fresh array per page. This
was the only hot paginated loop with the issue — getTradesPaginated and getBuilderTrades
already use single-page returns.


Note

Low Risk
Low risk: change is limited to how getTrades accumulates paginated results and adds defensive handling for missing page data, with new tests covering behavior and performance.

Overview
ClobClient.getTrades now accumulates paginated trades via in-place appends (push) instead of rebuilding arrays each page, eliminating quadratic copying/GC overhead on large histories.

Adds a guard to skip empty/undefined response.data pages, and introduces a vitest suite that mocks paging to verify ordering, cursor/only_first_page behavior, and includes a loose performance regression test to catch a return to O(N^2).

Reviewed by Cursor Bugbot for commit c17cc40. Bugbot is set up for automated code reviews on this repo. Configure here.

@KENILSHAHH KENILSHAHH requested a review from a team as a code owner April 14, 2026 17:35
@KENILSHAHH
Copy link
Copy Markdown
Author

@poly-rodr , @arun-poly any chance to get this reviewed ?

@arun-poly
Copy link
Copy Markdown
Contributor

@poly-rodr , @arun-poly any chance to get this reviewed ?

@Pradeep-selva can review this pls

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