Skip to content

Open source personal finance application

perf: replace O(I×N) interval bucketing with O(N log I) binary search

Ilya Komichev0b7bdbb8187b
diff --git a/app/components/stat/Item.vue b/app/components/stat/Item.vue
index 3dbeb433..1b76ffd6 100644
--- a/app/components/stat/Item.vue
+++ b/app/components/stat/Item.vue
@@ -76,6 +76,7 @@ function onClickCategory(clickedCategoryId: CategoryId) {
// Show quick view
quickViewTrns.value = trnsStore.getStoreTrnsIds({
categoriesIds: [clickedCategoryId],
+ sort: true,
trnsIds: selectedAndFilteredTrnsIds.value,
})
}
diff --git a/app/components/stat/useStatItem.ts b/app/components/stat/useStatItem.ts
index 5b6cc7f7..1c2481cc 100644
--- a/app/components/stat/useStatItem.ts
+++ b/app/components/stat/useStatItem.ts
@@ -6,7 +6,7 @@ import { differenceInDays, differenceInMonths, differenceInWeeks } from 'date-fn
import type { CategoryId } from '~/components/categories/types'
import type { Range, StatDateProvider } from '~/components/date/types'
import type { FilterProvider } from '~/components/stat/filter/types'
-import type { ChartSeries, IntervalData, SeriesSlugSelected, StatTabSlug } from '~/components/stat/types'
+import type { ChartSeries, SeriesSlugSelected, StatTabSlug } from '~/components/stat/types'
import type { StatConfigProvider } from '~/components/stat/useStatConfig'
import type { TrnId } from '~/components/trns/types'
@@ -82,26 +82,47 @@ export function useStatItem({
}))
function getIntervalsData(ids: TrnId[], intervalsInRange: Range[]) {
- return intervalsInRange.reduce((acc, range) => {
- const trnsIdsInRange = ids.filter((id) => {
- const trnDate = trnsStore.items?.[id]?.date
- return trnDate != null && trnDate >= range.start && trnDate <= range.end
- })
-
- acc.push({
- range,
- total: getTotalOfTrnsIds(trnsIdsInRange),
- trnsIds: trnsIdsInRange,
- })
-
- return acc
- }, [] as IntervalData[])
+ if (!intervalsInRange.length)
+ return []
+
+ // Pre-allocate one bucket per interval
+ const buckets: TrnId[][] = intervalsInRange.map(() => [])
+
+ // Single pass over trns — binary search for the matching interval
+ for (const id of ids) {
+ const trnDate = trnsStore.items?.[id]?.date
+ if (trnDate == null)
+ continue
+
+ let lo = 0
+ let hi = intervalsInRange.length - 1
+ while (lo <= hi) {
+ const mid = (lo + hi) >> 1
+ if (trnDate < intervalsInRange[mid].start) {
+ hi = mid - 1
+ }
+ else if (trnDate > intervalsInRange[mid].end) {
+ lo = mid + 1
+ }
+ else {
+ buckets[mid].push(id)
+ break
+ }
+ }
+ }
+
+ return intervalsInRange.map((range, i) => ({
+ range,
+ total: getTotalOfTrnsIds(buckets[i]),
+ trnsIds: buckets[i],
+ }))
}
const intervalsData = computed(() => getIntervalsData(rangeTrnsIds.value, statDate.intervalsInRange.value))
const intervalsDataWithFilteredCategories = computed(() => getIntervalsData(rangeTrnsIdsWithFilteredCategories.value, statDate.intervalsInRange.value))
const selectedTrnsIds = computed(() => trnsStore.getStoreTrnsIds({
+ sort: true,
trnsIds: statDate.params.value.intervalSelected !== -1
? intervalsData.value[statDate.params.value.intervalSelected]?.trnsIds
: rangeTrnsIds.value,
@@ -110,6 +131,7 @@ export function useStatItem({
const selectedAndFilteredTrnsIds = computed(() => trnsStore.getStoreTrnsIds({
categoriesIds: filteredCategoriesIds.value,
+ sort: true,
trnsIds: statDate.params.value.intervalSelected !== -1
? intervalsData.value[statDate.params.value.intervalSelected]?.trnsIds
: rangeTrnsIds.value,
diff --git a/app/components/trnForm/TrnsSlide.vue b/app/components/trnForm/TrnsSlide.vue
index 28ad2df4..a71820d5 100644
--- a/app/components/trnForm/TrnsSlide.vue
+++ b/app/components/trnForm/TrnsSlide.vue
@@ -43,6 +43,7 @@ const trnsIds = computed(() => {
return trnsStore.getStoreTrnsIds({
categoriesIds,
dates,
+ sort: true,
walletsIds,
})
})
diff --git a/app/components/trns/getTrns.ts b/app/components/trns/getTrns.ts
index 379e48b9..338dcb3d 100644
--- a/app/components/trns/getTrns.ts
+++ b/app/components/trns/getTrns.ts
@@ -40,7 +40,10 @@ export function getTrnsIds(props: TrnsGetterProps) {
: ids,
]
- return filters
- .reduce((ids, filter) => filter(ids), trnsIds)
- .sort((a, b) => props.trnsItems?.[b]?.date - props.trnsItems?.[a]?.date)
+ const result = filters.reduce((ids, filter) => filter(ids), trnsIds)
+
+ if (props.sort)
+ result.sort((a, b) => props.trnsItems?.[b]?.date - props.trnsItems?.[a]?.date)
+
+ return result
}
diff --git a/app/components/trns/pages/History.vue b/app/components/trns/pages/History.vue
index 62b43bed..8eb59928 100644
--- a/app/components/trns/pages/History.vue
+++ b/app/components/trns/pages/History.vue
@@ -19,6 +19,7 @@ useHead({
const trnsIds = computed(() => trnsStore.getStoreTrnsIds({
categoriesIds: filter.categoriesIds.value,
+ sort: true,
walletsIds: filter.walletsIds.value,
}))
diff --git a/app/components/trns/types.ts b/app/components/trns/types.ts
index f9b9f825..2a0b74bd 100644
--- a/app/components/trns/types.ts
+++ b/app/components/trns/types.ts
@@ -70,6 +70,7 @@ export type TrnsGetterProps = {
from: number
until: number
}
+ sort?: boolean
trnsIds?: TrnId[]
trnsItems?: Trns
trnsTypes?: TrnType[]
diff --git a/app/components/trns/useTrnsStore.ts b/app/components/trns/useTrnsStore.ts
index f75361e8..668a5c68 100644
--- a/app/components/trns/useTrnsStore.ts
+++ b/app/components/trns/useTrnsStore.ts
@@ -63,16 +63,28 @@ export const useTrnsStore = defineStore('trns', () => {
}
function getRange(trnsIds: TrnId[]): Range {
- if (!items.value) {
+ if (!items.value || !trnsIds.length) {
return {
end: getEndOf(new Date(), 'day').getTime(),
start: getStartOf(new Date(), 'day').getTime(),
}
}
+ let min = Infinity
+ let max = -Infinity
+ for (const id of trnsIds) {
+ const date = items.value[id]?.date
+ if (date != null) {
+ if (date < min)
+ min = date
+ if (date > max)
+ max = date
+ }
+ }
+
return {
- end: items.value[trnsIds.at(0)!]?.date ?? getEndOf(new Date(), 'day').getTime(),
- start: items.value[trnsIds.at(-1)!]?.date ?? getStartOf(new Date(), 'day').getTime(),
+ end: max !== -Infinity ? max : getEndOf(new Date(), 'day').getTime(),
+ start: min !== Infinity ? min : getStartOf(new Date(), 'day').getTime(),
}
}