Add Intl.NumberFormat cache.

This commit is contained in:
Franco Montenegro 2022-05-06 10:31:36 -03:00 committed by jessopb
parent efa682ef02
commit 3671e855cb
3 changed files with 20 additions and 3 deletions

View file

@ -1,3 +1,5 @@
import { getFormatter } from './intlNumberFormat';
export function formatCredits(amount, precision, shortFormat = false) { export function formatCredits(amount, precision, shortFormat = false) {
const language = localStorage.getItem('language') || undefined; const language = localStorage.getItem('language') || undefined;
const actualAmount = Number(amount); const actualAmount = Number(amount);
@ -6,7 +8,7 @@ export function formatCredits(amount, precision, shortFormat = false) {
if (Number.isNaN(actualAmount) || actualAmount === 0) return '0'; if (Number.isNaN(actualAmount) || actualAmount === 0) return '0';
if (shortFormat) { if (shortFormat) {
const formatter = new Intl.NumberFormat(language, { const formatter = getFormatter(language, {
minimumFractionDigits: safePrecision, minimumFractionDigits: safePrecision,
maximumFractionDigits: safePrecision, maximumFractionDigits: safePrecision,
roundingIncrement: 5, roundingIncrement: 5,
@ -17,7 +19,7 @@ export function formatCredits(amount, precision, shortFormat = false) {
return formatter.format(actualAmount); return formatter.format(actualAmount);
} }
const formatter = new Intl.NumberFormat(language, { const formatter = getFormatter(language, {
minimumFractionDigits: safePrecision, minimumFractionDigits: safePrecision,
maximumFractionDigits: safePrecision, maximumFractionDigits: safePrecision,
roundingIncrement: 5, roundingIncrement: 5,

View file

@ -0,0 +1,14 @@
// @flow
const formatterCache = {};
// Creating multiple Intl.NumberFormat instances can
// be expensive, that's why we perform a very simple
// cache.
// See https://github.com/formatjs/formatjs/issues/27#issuecomment-61148808
export function getFormatter(language?: string, options: any) {
const key = `${language || ''}:${JSON.stringify(options || {})}`;
if (!formatterCache[key]) {
formatterCache[key] = new Intl.NumberFormat(language, options);
}
return formatterCache[key];
}

View file

@ -1,9 +1,10 @@
// @flow // @flow
import { getFormatter } from './intlNumberFormat';
export function formatNumber(num: number, numberOfDigits?: number, short: boolean = false): string { export function formatNumber(num: number, numberOfDigits?: number, short: boolean = false): string {
const language = localStorage.getItem('language') || undefined; const language = localStorage.getItem('language') || undefined;
const safePrecision = Math.min(20, numberOfDigits || 0); const safePrecision = Math.min(20, numberOfDigits || 0);
const formatter = new Intl.NumberFormat(language, { const formatter = getFormatter(language, {
maximumFractionDigits: safePrecision, maximumFractionDigits: safePrecision,
notation: short ? 'compact' : 'standard', notation: short ? 'compact' : 'standard',
compactDisplay: 'short', compactDisplay: 'short',