Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

perf: remember week keys, Home's week tally keys today once

Duarte Santos89996c44b087
diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js
index 10406a322..bbae883f6 100644
--- a/frontend/src/lib/format.js
+++ b/frontend/src/lib/format.js
@@ -151,7 +151,22 @@ export function startOfWeek(iso, ws = MONDAY) {
* to be an ISO week number, and being one would be a liability here: "2026-35" is defined
* Monday-first, and there is no Sunday-first equivalent to fall back to.
*/
-export const weekKey = (iso, ws = MONDAY) => isoOf(startOfWeek(iso, ws))
+//
+// Memoised: Home's streak and week tally and Stats' weekly charts ask it for every workout on
+// every render, and each call parses a Date. The answer only depends on the calendar date and
+// the week start (noon local, so not on the time zone), so a string date is safe to remember.
+const weekKeys = new Map()
+export const weekKey = (iso, ws = MONDAY) => {
+ if (typeof iso !== 'string') return isoOf(startOfWeek(iso, ws))
+ const k = ws + '|' + iso
+ let v = weekKeys.get(k)
+ if (v === undefined) {
+ if (weekKeys.size >= 20000) weekKeys.clear()
+ v = isoOf(startOfWeek(iso, ws))
+ weekKeys.set(k, v)
+ }
+ return v
+}
export const localTZ = () => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC' } catch { return 'UTC' } }
diff --git a/frontend/src/lib/queue.js b/frontend/src/lib/queue.js
index 349e3a272..1179c9347 100644
--- a/frontend/src/lib/queue.js
+++ b/frontend/src/lib/queue.js
@@ -180,7 +180,8 @@ export function queueView(S, today) {
*/
export function weekTally(S, today) {
const ws = weekStartOf(S)
- const thisWeek = S.workouts.filter(w => weekKey(w.d, ws) === weekKey(today, ws))
+ const week = weekKey(today, ws)
+ const thisWeek = S.workouts.filter(w => weekKey(w.d, ws) === week)
const q0 = queueOf(S)
const q = q0 && today >= q0.startsOn ? q0 : null
// A weekday counts as planned only for routines that still exist (a pointer left behind by a
diff --git a/frontend/src/lib/week-start.test.js b/frontend/src/lib/week-start.test.js
index 5733b7d2a..17f55e98a 100644
--- a/frontend/src/lib/week-start.test.js
+++ b/frontend/src/lib/week-start.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect } from 'vitest'
+import { describe, it, expect, vi } from 'vitest'
import { MONDAY, SUNDAY, weekStartOf, weekOrder, weekDayOffset, startOfWeek, weekKey, isoOf } from './format.js'
import { streakWeeks } from './history.js'
import { muscleBalanceWindow } from './muscles.js'
@@ -71,6 +71,25 @@ describe('weekKey', () => {
it('does not collide across years', () => {
expect(weekKey('2025-08-19', MONDAY)).not.toBe(weekKey('2026-08-19', MONDAY))
})
+
+ // Home and Stats ask for every workout's week on each render; a date seen once is not parsed again.
+ it('remembers a date it has already keyed, and still answers like startOfWeek', () => {
+ const spy = vi.spyOn(Date.prototype, 'getDay')
+ try {
+ const first = weekKey('2031-02-12', SUNDAY)
+ const parsed = spy.mock.calls.length
+ expect(parsed).toBeGreaterThan(0)
+ expect(weekKey('2031-02-12', SUNDAY)).toBe(first)
+ expect(spy.mock.calls.length).toBe(parsed)
+ } finally { spy.mockRestore() }
+ for (let i = 0; i < 800; i += 1) {
+ const iso = isoOf(new Date(2024, 0, 1 + i, 12))
+ for (const ws of [MONDAY, SUNDAY]) {
+ expect(weekKey(iso, ws)).toBe(isoOf(startOfWeek(iso, ws)))
+ expect(weekKey(iso, ws)).toBe(isoOf(startOfWeek(iso, ws)))
+ }
+ }
+ })
})
describe('the week streak counts the profile’s own weeks', () => {