perf: replace O(n²) transaction matching with O(n) Map/Set lookups#205
Merged
hoiekim merged 4 commits intohoiekim:mainfrom Mar 28, 2026
Merged
Conversation
Contributor
Author
Self-ReviewDiscussion thread status:
Checked:
E2E Testing:
Issues found:
Confidence: High |
d852562 to
29f749b
Compare
Owner
|
@moltboie Add unit testing to ensure the same behavior |
29f749b to
160b2d0
Compare
Contributor
Author
|
Added unit tests. Exported |
hoiekim
reviewed
Mar 26, 2026
getRemovedTransactions and getRemovedInvestmentTransactions in sync-simple-fin.ts used Array.find() inside forEach, making them O(n²). The Plaid modelize and investment matching had the same pattern. Fix: pre-build lookup Maps/Sets before the loops so each lookup is O(1). - sync-simple-fin.ts: Set<transaction_id> and Set<investment_transaction_id> - sync-plaid.ts modelize: Map by transaction_id, pending_transaction_id, and compound key (account_id:name:amount) for the fallback case - sync-plaid.ts investments: Set<investment_transaction_id> Closes hoiekim#200
…entTransactions Exports both functions and adds 12 tests covering: - No removals (all present) - Identifying removed items - Ignoring items before startDate - Ignoring items from accounts not in incoming - Multiple removals - Empty stored list
160b2d0 to
ed47eed
Compare
investmentTransactions is PlaidInvestmentTransaction[] but the function expects JSONInvestmentTransaction[]. filledInvestments is the correctly typed mapped version (date strings filled in).
hoiekim
approved these changes
Mar 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces nested
Array.find()insideforEachloops with pre-builtMap/Setlookups in the Plaid and SimpleFin sync paths.Changes
sync-simple-fin.tsgetRemovedTransactions: pre-buildSet<transaction_id>→ O(1) lookupgetRemovedInvestmentTransactions: pre-buildSet<investment_transaction_id>→ O(1) lookupsync-plaid.tsmodelize: pre-build three maps beforeadded.map(modelize):Map<transaction_id, JSONTransaction>Map<pending_transaction_id, JSONTransaction>Map<${account_id}:${name}:${amount}, JSONTransaction>(for the compound-key fallback)Set<investment_transaction_id>→ O(1) lookupImpact
With hundreds of transactions per sync and multi-year history, the O(n²) pattern could cause noticeable slowdown. The Maps/Sets are built once per sync call (O(n)) rather than for each incoming transaction.
Testing
bun run buildpasses ✅Closes #200