Skip to content

Open source personal finance application

perf: replace O(W×N) wallet totals with single-pass O(N) computation

Ilya Komichev836f35b2a0af
diff --git a/app/components/amount/getTotal.test.ts b/app/components/amount/getTotal.test.ts
index 8d04a44e..68249b55 100644
--- a/app/components/amount/getTotal.test.ts
+++ b/app/components/amount/getTotal.test.ts
@@ -5,7 +5,7 @@ import { walletsItems } from '~~/mocks/wallets'
import type { TrnId } from '~/components/trns/types'
-import { getTotal } from '~/components/amount/getTotal'
+import { getTotal, getWalletsTotals } from '~/components/amount/getTotal'
describe('total of Transactions', () => {
it('correct empty result and correct total structure', () => {
@@ -180,3 +180,82 @@ describe('total of Transactions', () => {
expect(total.sumTransfers).toEqual(0)
})
})
+
+describe('getWalletsTotals', () => {
+ it('returns empty map for empty trns', () => {
+ const totals = getWalletsTotals({
+ trnsItems: {},
+ walletsItems,
+ })
+
+ expect(totals.size).toBe(0)
+ })
+
+ it('computes wallet balance from income and expense', () => {
+ const totals = getWalletsTotals({
+ trnsItems: {
+ t1: trnsItems.transactionIncomeWalletCashUSD1000,
+ t2: trnsItems.transactionExpenseWalletCashUSD400,
+ },
+ walletsItems,
+ })
+
+ expect(totals.get('walletCashUSD')).toBe(600)
+ })
+
+ it('handles transfers — adds to income wallet, subtracts from expense wallet', () => {
+ const totals = getWalletsTotals({
+ trnsItems: {
+ t1: trnsItems.transferExpenseWalletCashUSD10IncomeWalletRUB700,
+ },
+ walletsItems,
+ })
+
+ expect(totals.get('walletCashUSD')).toBe(-10)
+ expect(totals.get('walletRUB')).toBe(700)
+ })
+
+ it('handles adjustments as income/expense on wallet', () => {
+ const totals = getWalletsTotals({
+ trnsItems: {
+ t1: trnsItems.adjustmentIncomeWalletCashUSD200,
+ t2: trnsItems.adjustmentExpenseWalletCashUSD50,
+ },
+ walletsItems,
+ })
+
+ expect(totals.get('walletCashUSD')).toBe(150)
+ })
+
+ it('computes full balance for walletCashUSD with mixed trns', () => {
+ const totals = getWalletsTotals({
+ trnsItems: {
+ t1: trnsItems.transactionIncomeWalletCashUSD1000,
+ t2: trnsItems.transactionExpenseWalletCashUSD400,
+ t3: trnsItems.transferExpenseWalletCashUSD10IncomeWalletRUB700,
+ t4: trnsItems.transferExpenseWalletCreditUSD40IncomeWalletCashUSD40,
+ t5: trnsItems.adjustmentIncomeWalletCashUSD30,
+ t6: trnsItems.adjustmentExpenseWalletCashUSD30,
+ },
+ walletsItems,
+ })
+
+ // 1000 - 400 - 10 + 40 + 30 - 30 = 630
+ expect(totals.get('walletCashUSD')).toBe(630)
+ expect(totals.get('walletRUB')).toBe(700)
+ expect(totals.get('walletCreditUSD')).toBe(-40)
+ })
+
+ it('converts currencies when rates provided', () => {
+ const totals = getWalletsTotals({
+ baseCurrencyCode: 'USD',
+ rates,
+ trnsItems: {
+ t1: trnsItems.transactionIncomeWalletOneRUB700,
+ },
+ walletsItems,
+ })
+
+ expect(totals.get('walletOneRUB')).toBeCloseTo(10.81, 1)
+ })
+})
diff --git a/app/components/amount/getTotal.ts b/app/components/amount/getTotal.ts
index edde0266..501f7715 100644
--- a/app/components/amount/getTotal.ts
+++ b/app/components/amount/getTotal.ts
@@ -1,6 +1,6 @@
import type { CurrencyCode, Rates } from '~/components/currencies/types'
-import type { TrnId, TrnItem } from '~/components/trns/types'
-import type { WalletId, WalletItem } from '~/components/wallets/types'
+import type { TrnId, TrnItem, Trns } from '~/components/trns/types'
+import type { WalletId, WalletItem, Wallets } from '~/components/wallets/types'
import { TrnType } from '~/components/trns/types'
@@ -125,3 +125,49 @@ export function getTotal(props: TotalProps): TotalReturns {
sumTransfers,
}
}
+
+/**
+ * Single-pass wallet balance computation. O(N) instead of O(W×N).
+ * For each wallet, returns: income - expense + transfers + adjustments.
+ */
+export function getWalletsTotals(props: {
+ baseCurrencyCode?: string
+ rates?: Rates
+ trnsItems: Trns
+ walletsItems: Wallets
+}): Map<WalletId, number> {
+ const { baseCurrencyCode, rates, trnsItems, walletsItems } = props
+ const totals = new Map<WalletId, number>()
+
+ function addToWallet(walletId: WalletId, amount: number) {
+ totals.set(walletId, (totals.get(walletId) ?? 0) + amount)
+ }
+
+ function convert(amount: number, currencyCode: CurrencyCode): number {
+ return getAmountInRate({ amount, baseCurrencyCode, currencyCode, rates })
+ }
+
+ for (const trnId of Object.keys(trnsItems)) {
+ const trn = trnsItems[trnId]
+ if (!trn)
+ continue
+
+ if (trn.type === TrnType.Income || trn.type === TrnType.Expense) {
+ const wallet = walletsItems[trn.walletId]
+ if (!wallet)
+ continue
+ const amount = convert(trn.amount, wallet.currency)
+ addToWallet(trn.walletId, trn.type === TrnType.Income ? amount : -amount)
+ }
+ else if (trn.type === TrnType.Transfer && 'incomeWalletId' in trn) {
+ const incomeWallet = walletsItems[trn.incomeWalletId]
+ const expenseWallet = walletsItems[trn.expenseWalletId]
+ if (!incomeWallet || !expenseWallet)
+ continue
+ addToWallet(trn.incomeWalletId, convert(trn.incomeAmount, incomeWallet.currency))
+ addToWallet(trn.expenseWalletId, -convert(trn.expenseAmount, expenseWallet.currency))
+ }
+ }
+
+ return totals
+}
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index d145d3af..181f418a 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -5,7 +5,7 @@ import type { CurrencyCode } from '~/components/currencies/types'
import type { TrnId } from '~/components/trns/types'
import type { WalletId, WalletItem, WalletItemComputed, Wallets, WalletsComputed } from '~/components/wallets/types'
-import { getAmountInRate, getTotal } from '~/components/amount/getTotal'
+import { getAmountInRate, getWalletsTotals } from '~/components/amount/getTotal'
import { useCurrenciesStore } from '~/components/currencies/useCurrenciesStore'
import { useDemo } from '~/components/demo/useDemo'
import { pushOfflineOp } from '~/components/offline/helpers'
@@ -145,21 +145,6 @@ export const useWalletsStore = defineStore('wallets', () => {
})
}
- function getWalletTotal(walletId: WalletId) {
- const trnsIds = trnsStore.getStoreTrnsIds({
- walletsIds: [walletId],
- })
-
- const { adjustment, sum, sumTransfers } = getTotal({
- trnsIds,
- trnsItems: trnsStore.items ?? {},
- walletsIds: [walletId],
- walletsItems: items.value ?? {},
- })
-
- return sum + sumTransfers + adjustment
- }
-
const sortedIds = computed(() => {
if (!hasItems.value)
return []
@@ -169,11 +154,21 @@ export const useWalletsStore = defineStore('wallets', () => {
)
})
+ // O(N) single pass over all trns — replaces O(W×N) per-wallet getWalletTotal
+ const walletTotals = computed(() =>
+ getWalletsTotals({
+ baseCurrencyCode: currenciesStore.base,
+ rates: currenciesStore.rates,
+ trnsItems: trnsStore.items ?? {},
+ walletsItems: items.value ?? {},
+ }),
+ )
+
const itemsComputed = computed<WalletsComputed>(() =>
sortedIds.value.reduce((acc, id) => {
acc[id] = {
...items.value[id],
- amount: getWalletTotal(id),
+ amount: walletTotals.value.get(id) ?? 0,
rate: getAmountInRate({
amount: 1,
baseCurrencyCode: currenciesStore.base,