963 markdown files reformatted with the repository's pinned prettier so pnpm format:check covers the folded tree like every other repo file. The formatter's embedded-language pass also normalized code fences (TS semicolons, closed HTML tags in examples, lowercased CSS hex colors, one renumbered list that skipped an index). Alphanumeric token deltas vs the fold commit were audited file-by-file; all are formatter-equivalent markup normalizations plus the four sanitized skills.
1.6 KiB
1.6 KiB
title, impact, impactDescription, tags
| title | impact | impactDescription | tags |
|---|---|---|---|
| Hoist Intl Formatter Creation | LOW-MEDIUM | avoids expensive object recreation | javascript, intl, optimization, memoization |
Hoist Intl Formatter Creation
Don't create Intl.DateTimeFormat, Intl.NumberFormat, or
Intl.RelativeTimeFormat inside render or loops. These are expensive to
instantiate. Hoist to module scope when the locale/options are static.
Incorrect (new formatter every render):
function Price({ amount }: { amount: number }) {
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
return <Text>{formatter.format(amount)}</Text>;
}
Correct (hoisted to module scope):
const currencyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
function Price({ amount }: { amount: number }) {
return <Text>{currencyFormatter.format(amount)}</Text>;
}
For dynamic locales, memoize:
const dateFormatter = useMemo(
() => new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }),
[locale],
);
Common formatters to hoist:
// Module-level formatters
const dateFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' });
const timeFormatter = new Intl.DateTimeFormat('en-US', { timeStyle: 'short' });
const percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' });
const relativeFormatter = new Intl.RelativeTimeFormat('en-US', {
numeric: 'auto',
});
Creating Intl objects is significantly more expensive than RegExp or plain
objects—each instantiation parses locale data and builds internal lookup tables.