unsplash / intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
MIT License
57 stars 3 forks source link

Avoid repeated JS instantiations #22

Open samhh opened 2 years ago

samhh commented 2 years ago

e.g. if the same number is used twice we'll compile two instantiations of Intl.NumberFormat. The compiler knows enough to potentially open a function body and declare a variable.

OliverJAsh commented 2 years ago

Perhaps we could create instances (e.g. for Intl.NumberFormat) once in the module scope and then re-use them, e.g.:

const numberFormat = new Intl.NumberFormat('en-US')

export const a = (params) => `${numberFormat.format(params.number)}k`
export const b = (params) => `Age: ${numberFormat.format(params.number)}`

Although, I think we may need different instances because we need to pass through the options from the variables in the ICU message—e.g. a single message may have both a plain number and a number to be formatted as currency.

FormatJS has createIntlCache which is intended to solve a similar problem: