Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(charts): cache timezone date parsing across charts (#125613)

Matt Puafd488c6f666d
diff --git a/packages/quill/packages/charts/src/utils/dayjs.test.ts b/packages/quill/packages/charts/src/utils/dayjs.test.ts
index 4dfbd36f7080..cab46add3799 100644
--- a/packages/quill/packages/charts/src/utils/dayjs.test.ts
+++ b/packages/quill/packages/charts/src/utils/dayjs.test.ts
@@ -48,6 +48,7 @@ describe('parseDateInTimezone', () => {
const tokyo = parseDateInTimezone('2026-03-08', 'Asia/Tokyo')
expect(utc.format('YYYY-MM-DD')).toBe('2026-03-08')
expect(tokyo.format('YYYY-MM-DD')).toBe('2026-03-08')
+ expect([utc.format('Z'), tokyo.format('Z')]).toEqual(['+00:00', '+09:00'])
})
})
diff --git a/packages/quill/packages/charts/src/utils/dayjs.ts b/packages/quill/packages/charts/src/utils/dayjs.ts
index a85bb37ce546..da5b9410a389 100644
--- a/packages/quill/packages/charts/src/utils/dayjs.ts
+++ b/packages/quill/packages/charts/src/utils/dayjs.ts
@@ -16,6 +16,9 @@ dayjs.extend(timezone)
export { dayjs }
export interface Dayjs extends DayjsOriginal {}
+const PARSED_DATES_LIMIT = 5_000
+const parsedDates = new Map<string, Dayjs>()
+
/** Parse a date string into a Dayjs in the given timezone, browser-tz-independent.
*
* - Strings without explicit timezone info ("2026-03-08", "2026-03-08 14:00:00")
@@ -23,6 +26,22 @@ export interface Dayjs extends DayjsOriginal {}
* - Strings with explicit timezone info (trailing "Z" or "±HH:MM") are real instants;
* parse them as such and convert into the requested timezone. */
export function parseDateInTimezone(dateStr: string, tz: string): Dayjs {
+ // Timezone parsing goes through Intl and is slow. Charts on one dashboard share most labels,
+ // and a chart parses all of them again each time it mounts. Dayjs instances are immutable,
+ // so a cached instance is safe to share.
+ const key = `${tz}|${dateStr}`
+ let parsed = parsedDates.get(key)
+ if (!parsed) {
+ if (parsedDates.size >= PARSED_DATES_LIMIT) {
+ parsedDates.clear()
+ }
+ parsed = parseUncached(dateStr, tz)
+ parsedDates.set(key, parsed)
+ }
+ return parsed
+}
+
+function parseUncached(dateStr: string, tz: string): Dayjs {
const hasExplicitTz = /([Zz]|[+-]\d{2}:?\d{2})$/.test(dateStr)
try {
if (hasExplicitTz) {