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.

exercise search: cache the normalised haystack per exercise

Duarte Santos669756a6dd6d
diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.js
index 197b3fc07..f4b41d536 100644
--- a/frontend/src/lib/exercises.js
+++ b/frontend/src/lib/exercises.js
@@ -1,5 +1,5 @@
import { EXDB } from './exercises-data.js'
-import { t } from './i18n-core.js'
+import { t, getVersion } from './i18n-core.js'
export { EXDB }
@@ -88,23 +88,36 @@ export const normalizeStr = s => (s || '')
// Matches when all whitespace-separated words in the query appear anywhere in the exercise's
// name, equipment, target muscle, body part (both in English and translated to active language),
// secondary muscles or description.
-export function matchExercise(e, query) {
- if (!query) return true
- const tokens = normalizeStr(query).split(/\s+/).filter(Boolean)
- if (!tokens.length) return true
+//
+// The haystack is built once per exercise and cached: NFD-normalising ~1300 catalogue entries
+// on every keystroke costs ~8ms on a desktop and several times that on a phone. The cache key
+// is the i18n version (bumped by every setLang), so switching language rebuilds the translated
+// terms. Custom exercises are re-cached automatically — the store clones state on update, so an
+// edited exercise arrives as a new object the WeakMap has never seen.
+const corpusCache = new WeakMap()
- const corpus = normalizeStr([
+function corpusOf(e) {
+ const v = getVersion()
+ const hit = corpusCache.get(e)
+ if (hit && hit.v === v) return hit.s
+ const sm = Array.isArray(e?.sm) ? e.sm : []
+ const s = normalizeStr([
e?.n || '',
- e?.tg || '',
- t(e?.tg || ''),
- e?.eq || '',
- t(e?.eq || ''),
- e?.bp || '',
- t(e?.bp || ''),
- ...(Array.isArray(e?.sm) ? e.sm : []),
- ...(Array.isArray(e?.sm) ? e.sm.map(m => t(m)) : []),
+ e?.tg || '', t(e?.tg || ''),
+ e?.eq || '', t(e?.eq || ''),
+ e?.bp || '', t(e?.bp || ''),
+ ...sm, ...sm.map(m => t(m)),
e?.desc || ''
].join(' '))
+ corpusCache.set(e, { v, s })
+ return s
+}
+export function matchExercise(e, query) {
+ if (!query) return true
+ const tokens = normalizeStr(query).split(/\s+/).filter(Boolean)
+ if (!tokens.length) return true
+ if (!e || typeof e !== 'object') return false
+ const corpus = corpusOf(e)
return tokens.every(tok => corpus.includes(tok))
}