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: keep the Intl formatters behind fmtDate, fmtNum and fmtPlate

Duarte Santosc4173eff1937
diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js
index 20c3d1420..caec1c61e 100644
--- a/frontend/src/lib/format.js
+++ b/frontend/src/lib/format.js
@@ -12,11 +12,26 @@ export const DAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
export const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
export const MONTHS_LONG = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
+// toLocaleDateString / toLocaleString build a new Intl formatter on every call, which is most of
+// what a long list of dates and weights costs (History with 400 workouts: ~430 ms in fmtDate
+// alone). The formatters are kept per locale and options; their output is the same text.
+const formatters = new Map()
+const formatter = (Kind, locale, options) => {
+ const key = Kind.name + '|' + locale + '|' + (options ? JSON.stringify(options) : '')
+ let f = formatters.get(key)
+ if (!f) { f = new Kind(locale, options); formatters.set(key, f) }
+ return f
+}
+const DATE_SHORT = { day: 'numeric', month: 'short' }
+const DATE_LONG = { weekday: 'short', day: 'numeric', month: 'short' }
+const DATE_SHORT_Y = { ...DATE_SHORT, year: 'numeric' }
+const DATE_LONG_Y = { ...DATE_LONG, year: 'numeric' }
export function fmtDate(iso, long, withYear = false) {
const d = new Date(iso + 'T12:00:00')
- const options = long ? { weekday: 'short', day: 'numeric', month: 'short' } : { day: 'numeric', month: 'short' }
- if (withYear) options.year = 'numeric'
- return d.toLocaleDateString(dateLocale(), options)
+ const options = long ? (withYear ? DATE_LONG_Y : DATE_LONG) : (withYear ? DATE_SHORT_Y : DATE_SHORT)
+ // An unreadable date says "Invalid Date" as it always did; a formatter would throw instead.
+ if (Number.isNaN(d.getTime())) return d.toLocaleDateString(dateLocale(), options)
+ return formatter(Intl.DateTimeFormat, dateLocale(), options).format(d)
}
// A span of days, e.g. an import's first and last workout. The year is shown when the span
// crosses one or lies outside the current year: "3 Feb – 21 Dec" over two years read as one.
@@ -57,10 +72,12 @@ export const setWeightDecimals = n => { decimals = n === 2 ? 2 : 1 }
export const weightDecimals = () => decimals
export const fmtNum = n => {
const p = decimals === 2 ? 100 : 10
- return (Math.round(n * p) / p).toLocaleString(dateLocale(), { maximumFractionDigits: decimals })
+ return formatter(Intl.NumberFormat, dateLocale(), decimals === 2 ? NUM_2 : NUM_1).format(Math.round(n * p) / p)
}
+const NUM_1 = { maximumFractionDigits: 1 }
+const NUM_2 = { maximumFractionDigits: 2 }
// Plate sizes keep their quarter: 1.25 and 21.25 are real numbers on a plate and a bar.
-export const fmtPlate = n => (Math.round(n * 100) / 100).toLocaleString(dateLocale())
+export const fmtPlate = n => formatter(Intl.NumberFormat, dateLocale()).format(Math.round(n * 100) / 100)
// Volume stays in the profile's unit throughout: the old shorthand turned anything over
// 10 000 into "t", which is wrong for a pound profile and made one list mix "18.8t" with
// "7'535 kg" — two numbers you can't compare at a glance.