Rotator: reuse one Intl.DateTimeFormat per time zone in cellAt (bestHours built one per hit; a 10k-hit link's stats took ~4 s and blocked the event loop)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+9
-1
@@ -60,9 +60,17 @@ function cleanTz(tz) {
|
|||||||
}
|
}
|
||||||
const zoneFor = (r, country) => (r.tz && r.tz !== 'visitor') ? r.tz : (COUNTRY_TZ[String(country || '').toUpperCase()] || DEFAULT_TZ);
|
const zoneFor = (r, country) => (r.tz && r.tz !== 'visitor') ? r.tz : (COUNTRY_TZ[String(country || '').toUpperCase()] || DEFAULT_TZ);
|
||||||
// the weekday + hour right now (or at ts) in a zone -> { wd, h }
|
// the weekday + hour right now (or at ts) in a zone -> { wd, h }
|
||||||
|
// one formatter per time zone: building an Intl.DateTimeFormat is expensive, and bestHours calls cellAt once per hit
|
||||||
|
// (2026-10-14: a 10k-hit link took ~4 s to open its stats and blocked every other request meanwhile)
|
||||||
|
const TZ_FMT = new Map();
|
||||||
|
function tzFormatter(tz) {
|
||||||
|
let f = TZ_FMT.get(tz);
|
||||||
|
if (!f) { f = new Intl.DateTimeFormat('en-US', { timeZone: tz, hour: 'numeric', hour12: false, hourCycle: 'h23', weekday: 'short' }); TZ_FMT.set(tz, f); }
|
||||||
|
return f;
|
||||||
|
}
|
||||||
function cellAt(tz, ts) {
|
function cellAt(tz, ts) {
|
||||||
try {
|
try {
|
||||||
const parts = new Intl.DateTimeFormat('en-US', { timeZone: tz, hour: 'numeric', hour12: false, hourCycle: 'h23', weekday: 'short' }).formatToParts(new Date(ts || Date.now()));
|
const parts = tzFormatter(tz).formatToParts(new Date(ts || Date.now()));
|
||||||
let wd = 1, h = 0; for (const p of parts) { if (p.type === 'weekday') wd = WD[p.value] ?? 1; if (p.type === 'hour') h = Number(p.value) % 24; }
|
let wd = 1, h = 0; for (const p of parts) { if (p.type === 'weekday') wd = WD[p.value] ?? 1; if (p.type === 'hour') h = Number(p.value) % 24; }
|
||||||
return { wd, h };
|
return { wd, h };
|
||||||
} catch (e) { return cellAt(DEFAULT_TZ, ts); }
|
} catch (e) { return cellAt(DEFAULT_TZ, ts); }
|
||||||
|
|||||||
Reference in New Issue
Block a user