From 3cbfd6854d68499b8d218dc0c28c7cdc6361b5fe Mon Sep 17 00:00:00 2001 From: piggei Date: Fri, 30 Oct 2026 21:51:04 +0100 Subject: [PATCH] perf(portfolio): cache parsed dates in MWR calculations --- .../calculator/mwr/portfolio-calculator.ts | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/mwr/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/mwr/portfolio-calculator.ts index ca0233c85c..2048429a8d 100644 --- a/apps/api/src/app/portfolio/calculator/mwr/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/mwr/portfolio-calculator.ts @@ -29,6 +29,21 @@ interface DatedCashFlow { * of the dated investor cash flows and the terminal portfolio value. */ export class MwrPortfolioCalculator extends PortfolioCalculator { + declare private parsedDates: Map | undefined; + + private getParsedDate(date: string): Date { + this.parsedDates ??= new Map(); + + let parsedDate = this.parsedDates.get(date); + + if (!parsedDate) { + parsedDate = parseDate(date); + this.parsedDates.set(date, parsedDate); + } + + return parsedDate; + } + protected calculateOverallPerformance({ accumulatedValuesByDate, positions @@ -372,8 +387,8 @@ export class MwrPortfolioCalculator extends PortfolioCalculator { return 0; } - const firstDate = parseDate(aggregatedCashFlows[0].date); - const lastDate = parseDate(aggregatedCashFlows.at(-1).date); + const firstDate = this.getParsedDate(aggregatedCashFlows[0].date); + const lastDate = this.getParsedDate(aggregatedCashFlows.at(-1).date); if (differenceInCalendarDays(lastDate, firstDate) === 0) { return 0; @@ -388,7 +403,8 @@ export class MwrPortfolioCalculator extends PortfolioCalculator { } const normalizedCashFlows = aggregatedCashFlows.map(({ date, value }) => ({ - exponent: differenceInCalendarDays(parseDate(date), firstDate) / 365, + exponent: + differenceInCalendarDays(this.getParsedDate(date), firstDate) / 365, value: value.div(scale).toNumber() }));