Skip to content

perf: replace O(n²) concat with push in kalshi fetcher#603

Merged
realfishsam merged 1 commit into
mainfrom
fix/305-343-kalshi-fetcher
May 24, 2026
Merged

perf: replace O(n²) concat with push in kalshi fetcher#603
realfishsam merged 1 commit into
mainfrom
fix/305-343-kalshi-fetcher

Conversation

@realfishsam
Copy link
Copy Markdown
Contributor

Fixes #343

@realfishsam
Copy link
Copy Markdown
Contributor Author

PR Review: VERIFIED

What This Does

Replaces three allEvents = allEvents.concat(events) calls with allEvents.push(...events) in the Kalshi fetcher's paginated event-fetching loops. Both produce the same result, but push mutates the existing array in-place instead of allocating a new copy on every page.

Blast Radius

Minimal. Three lines changed in core/src/exchanges/kalshi/fetcher.ts. The call sites (fetchEventsForMarkets, fetchAllWithStatus, fetchPageWithStatus) are internal pagination loops where allEvents is a local let array -- the old code was already mutating the binding via reassignment, so switching to push is semantically equivalent and avoids O(n) copies per page.

Findings

  • No issues found. The change is correct and safe. push(...spread) is the standard pattern for accumulating into a local array.
  • One edge-case to be aware of: if events ever contained tens of thousands of elements in a single page, push(...events) could hit the call-stack argument limit (~65k). Given BATCH_SIZE governs page size, this is not a realistic concern here.

Semver Impact

patch -- internal perf improvement, no API surface change.

Risk

Low.

@realfishsam realfishsam merged commit d3efc2f into main May 24, 2026
9 of 12 checks passed
@realfishsam realfishsam deleted the fix/305-343-kalshi-fetcher branch May 24, 2026 17:03
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.

Unbounded: core/src/exchanges/kalshi/fetcher.ts — O(n²) array.concat() with MAX_PAGES=1000

1 participant