Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build performant React Native and Expo apps with best practices for lists, animations, and navigation
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/js-hoist-intl.md
1---2title: Hoist Intl Formatter Creation3impact: LOW-MEDIUM4impactDescription: avoids expensive object recreation5tags: javascript, intl, optimization, memoization6---78## Hoist Intl Formatter Creation910Don't create `Intl.DateTimeFormat`, `Intl.NumberFormat`, or11`Intl.RelativeTimeFormat` inside render or loops. These are expensive to12instantiate. Hoist to module scope when the locale/options are static.1314**Incorrect (new formatter every render):**1516```tsx17function Price({ amount }: { amount: number }) {18const formatter = new Intl.NumberFormat('en-US', {19style: 'currency',20currency: 'USD',21})22return <Text>{formatter.format(amount)}</Text>23}24```2526**Correct (hoisted to module scope):**2728```tsx29const currencyFormatter = new Intl.NumberFormat('en-US', {30style: 'currency',31currency: 'USD',32})3334function Price({ amount }: { amount: number }) {35return <Text>{currencyFormatter.format(amount)}</Text>36}37```3839**For dynamic locales, memoize:**4041```tsx42const dateFormatter = useMemo(43() => new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }),44[locale]45)46```4748**Common formatters to hoist:**4950```tsx51// Module-level formatters52const dateFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' })53const timeFormatter = new Intl.DateTimeFormat('en-US', { timeStyle: 'short' })54const percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' })55const relativeFormatter = new Intl.RelativeTimeFormat('en-US', {56numeric: 'auto',57})58```5960Creating `Intl` objects is significantly more expensive than `RegExp` or plain61objects—each instantiation parses locale data and builds internal lookup tables.62