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: update() copies only the fields a change touches

Duarte Santos092d59cd2619
diff --git a/frontend/src/store/useStore.draft.test.jsx b/frontend/src/store/useStore.draft.test.jsx
new file mode 100644
index 000000000..9a9165608
--- /dev/null
+++ b/frontend/src/store/useStore.draft.test.jsx
@@ -0,0 +1,66 @@
+// @vitest-environment happy-dom
+// update() hands its producer a copy-on-access draft (draftOf): a field the producer reads or
+// writes is a deep copy, every other field stays the previous state's own object. What it must
+// keep from the old full clone: the previous state is never changed, a change lands, and what is
+// saved is the same document a full clone would have saved.
+import { describe, expect, it, vi } from 'vitest'
+
+vi.mock('../lib/api.js', () => ({ api: vi.fn() }))
+
+import { draftOf, useStore } from './useStore.js'
+
+const base = () => ({ unit: 'kg', n: 3, gone: undefined, bad: NaN, workouts: [{ id: 'w1', sets: [1, 2] }], active: { cur: 0, entries: [{ id: 'a', sets: [] }] }, week: { 1: 'r1' } })
+
+describe('draftOf', () => {
+ it('shares untouched fields, copies touched ones, and leaves the previous state alone', () => {
+ const prev = base()
+ const d = draftOf(prev)
+ d.S.active.cur = 2
+ d.S.active.entries[0].sets.push({ w: 50 })
+ const S = d.done()
+ expect(S.workouts).toBe(prev.workouts)
+ expect(S.week).toBe(prev.week)
+ expect(S.active).not.toBe(prev.active)
+ expect(prev.active).toEqual({ cur: 0, entries: [{ id: 'a', sets: [] }] })
+ expect(S.active.entries[0].sets).toEqual([{ w: 50 }])
+ })
+
+ it('writes the same document a full JSON clone would', () => {
+ const prev = base()
+ const d = draftOf(prev)
+ d.S.unit = 'lb'
+ d.S.workouts.push({ id: 'w2' })
+ const S = d.done()
+ const full = JSON.parse(JSON.stringify(prev)); full.unit = 'lb'; full.workouts.push({ id: 'w2' })
+ expect(JSON.stringify(S)).toBe(JSON.stringify(full))
+ expect(prev.workouts).toHaveLength(1)
+ expect('gone' in S).toBe(false)
+ expect(S.bad).toBeNull()
+ })
+
+ it('takes assignments and deletions of fields never read', () => {
+ const prev = base()
+ const d = draftOf(prev)
+ d.S.week = { 2: 'r2' }
+ delete d.S.workouts
+ d.S.added = { x: 1 }
+ const S = d.done()
+ expect(S.week).toEqual({ 2: 'r2' })
+ expect('workouts' in S).toBe(false)
+ expect(S.added).toEqual({ x: 1 })
+ expect(prev.week).toEqual({ 1: 'r1' })
+ expect(Object.keys(S)).toEqual(['unit', 'n', 'bad', 'active', 'week', 'added'])
+ })
+
+ it('keeps the history of a ticked set the same object through update()', () => {
+ useStore.getState().update(s => { s.workouts = [{ id: 'w1', d: '2026-10-01', exercises: [] }]; s.active = { cur: 0, entries: [] } }, false)
+ const before = useStore.getState().S
+ useStore.getState().update(s => { s.active.cur = 1 }, false)
+ const after = useStore.getState().S
+ expect(after).not.toBe(before)
+ expect(after.workouts).toBe(before.workouts)
+ expect(after.active.cur).toBe(1)
+ expect(before.active.cur).toBe(0)
+ expect(JSON.parse(localStorage.getItem('gym_state_v1')).active.cur).toBe(1)
+ })
+})
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index b5f54be7e..44a03ba7f 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -244,6 +244,37 @@ export const DEF = {
}
const clone = o => JSON.parse(JSON.stringify(o))
+// The draft an update() hands its producer: a copy of the state in which each top-level field is
+// copied (deep, as clone does) the first time the producer reads or writes it, and every field it
+// never touched stays the very object the previous state holds. A ticked set touches `active` and
+// little else, so it no longer copies the whole history (a full clone of a 400-workout profile was
+// most of the ~200 ms each tick took). The untouched fields are the old state's own objects, which
+// nothing mutates (every change goes through a draft like this one), and keeping them means that
+// what reads them by identity — the stamps (sync-merge stampChange), the watch's ready sessions,
+// a component's selector — sees them as unchanged, which they are. `done()` turns the fields still
+// untouched into plain values; until then reading one copies it, as clone() would have.
+export function draftOf(prev) {
+ const S = {}
+ const plain = (k, value) => Object.defineProperty(S, k, { value, writable: true, enumerable: true, configurable: true })
+ for (const k of Object.keys(prev)) {
+ const v = prev[k]
+ if (v === undefined || typeof v === 'function' || typeof v === 'symbol') continue
+ if (v === null || typeof v !== 'object') { S[k] = typeof v === 'number' && !Number.isFinite(v) ? null : v; continue }
+ Object.defineProperty(S, k, {
+ enumerable: true, configurable: true,
+ get() { const c = clone(v); plain(k, c); return c },
+ set(x) { plain(k, x) },
+ })
+ }
+ return {
+ S,
+ done() {
+ for (const k of Object.keys(S)) if (Object.getOwnPropertyDescriptor(S, k)?.get) plain(k, prev[k])
+ return S
+ },
+ }
+}
+
// First run on a device whose language renders right-to-left starts in that language
// rather than English; the boot script in index.html mirrors this check for the
// pre-paint direction. The choice is persisted like any other setting once the user
@@ -1346,8 +1377,9 @@ export const useStore = create((set, get) => {
},
update(mut, push = true) {
const prev = get().S
- const S = clone(prev)
- mut(S)
+ const draft = draftOf(prev)
+ mut(draft.S)
+ const S = draft.done()
// What the change removed, and which routines, workouts, settings and plan days it touched,
// each with one time that comes after every stamp the copy already carries: a conflict then
// keeps a removal and the edit made last, even from a device whose clock runs behind