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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Workout: the best-set line finds the best set in one pass over the history instead of copying and sorting it on every tap (#173)
Duarte Santos51e1d8d6d990
diff --git a/frontend/src/lib/exercise-history.js b/frontend/src/lib/exercise-history.jsindex a088dab4e..0063d3fb2 100644--- a/frontend/src/lib/exercise-history.js+++ b/frontend/src/lib/exercise-history.js@@ -108,15 +108,22 @@ export function bestSetFor(S, exId, mode = modeOf({ id: exId })) {if (a1 !== b1) return mode === 'reps' ? beatsWeight(exId, a1, b1) : a1 > b1return a2 > b2}+ // One pass over the history as it is stored, with no copy and no sort: this runs on every+ // render of every exercise card while the line shows the best set, and every stepper tap+ // re-renders them all, so sorting a long imported history each time was the cost of a tap.+ // Stored order is not always the order things happened in (a moved session keeps its place+ // until it is filed again), so a tie is settled by the start time, not by which came first+ // in the array; within one session the set logged first keeps it.let best = null- const logged = [...(S?.workouts || [])].sort((a, b) => startOf(a) - startOf(b))- for (const w of logged) {+ for (const w of S?.workouts || []) {+ let t = nullfor (const en of w.entries || []) {if (en.id !== exId) continuefor (const set of metricRowsForEntry(en, mode)) {- if (!best || better(set, best.set)) best = { d: w.d, set, target: en.target || null }+ t ??= startOf(w)+ if (!best || better(set, best.set) || (!better(best.set, set) && t < best.t)) best = { d: w.d, set, target: en.target || null, t }}}}- return best+ return best && { d: best.d, set: best.set, target: best.target }}diff --git a/frontend/src/lib/exercise-history.test.js b/frontend/src/lib/exercise-history.test.jsindex 36b3492f6..66a593f7c 100644--- a/frontend/src/lib/exercise-history.test.js+++ b/frontend/src/lib/exercise-history.test.js@@ -1,4 +1,4 @@-import { describe, it, expect } from 'vitest'+import { describe, it, expect, vi } from 'vitest'import { exerciseHistory, HISTORY_SESSIONS, bestSetFor } from './exercise-history.js'import { EXDB } from './exercises-data.js'import { estimate1RM, e1rmSeries } from './onerm.js'@@ -205,6 +205,20 @@ describe('bestSetFor', () => {expect(bestSetFor({ workouts: [run(0, 30, 9), run(1, 30, 10), run(2, 25, 12)] }, 'run', 'cardio').set).toMatchObject({ min: 30, speed: 10 })})+ // Asked on every render of every card while the line shows the best set, and each stepper tap+ // re-renders them all: copying and sorting a long history each time was the cost of a tap.+ it('settles a tie by start time in one pass, without sorting the history', () => {+ const at = (i, rid) => ({ ...session(i, [work(80, 5)]), entries: [{ id: 'bench', rid, target: { mode: 'reps' }, sets: [work(80, 5)] }] })+ const workouts = [at(5, 'c'), at(3, 'b'), at(1, 'a'), session(4, [work(70, 10)])]+ const sort = vi.spyOn(Array.prototype, 'sort')+ try {+ expect(bestSetFor({ workouts }, 'bench', 'reps')).toMatchObject({ d: iso(1), target: { mode: 'reps' } })+ expect(sort).not.toHaveBeenCalled()+ } finally { sort.mockRestore() }+ // a heavier set later on still beats an earlier tie+ expect(bestSetFor({ workouts: [...workouts, session(6, [work(85, 1)])] }, 'bench', 'reps')).toMatchObject({ d: iso(6), set: { w: 85, r: 1 } })+ })+it('takes the least help on an assistance machine, and nothing when nothing was logged', () => {const id = EXDB.find(e => e.eq === 'leverage machine' && /assist/i.test(e.n)).idconst S = { workouts: [0, 1, 2].map(i => ({ id: 'a' + i, d: iso(i), start: T0 + i * DAY,