Skip to content

Commit 303a4ea

Browse files
authored
feat: pass in formatters to IntlMessageFormat for perf (#1343)
* feat: pass in formatters to IntlMessageFormat for perf * fix up benchmark
1 parent 58a1739 commit 303a4ea

File tree

11 files changed

+314
-221
lines changed

11 files changed

+314
-221
lines changed

package-lock.json

Lines changed: 76 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
},
6464
"dependencies": {
6565
"@types/hoist-non-react-statics": "^3.3.1",
66-
"@types/invariant": "^2.2.29",
66+
"@types/invariant": "^2.2.30",
6767
"@types/react": "^16.8.20",
6868
"hoist-non-react-statics": "^3.3.0",
69-
"intl-format-cache": "^3.2.0",
70-
"intl-locales-supported": "^1.2.0",
71-
"intl-messageformat": "^4.0.0",
69+
"intl-format-cache": "^3.3.0",
70+
"intl-locales-supported": "^1.3.0",
71+
"intl-messageformat": "^4.3.0",
7272
"invariant": "^2.1.1",
7373
"react": "^16.3.0",
7474
"shallow-equal": "^1.1.0"
@@ -83,14 +83,16 @@
8383
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
8484
"@babel/preset-env": "^7.1.0",
8585
"@babel/preset-react": "^7.0.0",
86-
"@formatjs/intl-relativetimeformat": "^2.1.0",
87-
"@types/enzyme": "^3.9.4",
86+
"@formatjs/intl-relativetimeformat": "^2.3.0",
87+
"@types/benchmark": "^1.0.31",
88+
"@types/enzyme": "^3.10.1",
8889
"@types/jest": "^24.0.13",
8990
"@types/prop-types": "^15.7.1",
91+
"@types/react-dom": "^16.8.4",
9092
"@typescript-eslint/eslint-plugin": "^1.10.2",
9193
"@typescript-eslint/parser": "^1.10.2",
9294
"babel-jest": "^24.8.0",
93-
"babel-plugin-react-intl": "^3.2.1",
95+
"babel-plugin-react-intl": "^3.5.0",
9496
"babel-plugin-transform-member-expression-literals": "^6.9.4",
9597
"babel-plugin-transform-property-literals": "^6.9.4",
9698
"babel-plugin-transform-react-remove-prop-types": "^0.4.18",
@@ -108,7 +110,7 @@
108110
"full-icu": "^1.3.0",
109111
"glob": "^7.0.0",
110112
"intl": "^1.2.1",
111-
"intl-messageformat-parser": "^1.7.0",
113+
"intl-messageformat-parser": "^1.8.0",
112114
"intl-pluralrules": "^1.0.3",
113115
"jest": "^24.8.0",
114116
"markdown-toc": "^1.2.0",
@@ -120,7 +122,7 @@
120122
"prop-types": "^15.7.2",
121123
"react-dom": "^16.8.6",
122124
"rimraf": "^2.4.2",
123-
"rollup": "^1.16.3",
125+
"rollup": "^1.16.4",
124126
"rollup-plugin-commonjs": "^10.0.1",
125127
"rollup-plugin-node-resolve": "^5.2.0",
126128
"rollup-plugin-replace": "^2.0.0",
@@ -129,11 +131,13 @@
129131
"serialize-javascript": "^1.5.0",
130132
"superagent": "^5.0.8",
131133
"ts-jest": "^24.0.2",
134+
"ts-node": "^8.3.0",
132135
"tslib": "^1.9.3",
133136
"typescript": "^3.5.1",
134137
"watchify": "^3.7.0"
135138
},
136139
"scripts": {
140+
"benchmark": "TS_NODE_PROJECT='test/perf/tsconfig.json' ts-node test/perf/index.tsx",
137141
"build:dist:dev": "cross-env NODE_ENV=development rollup -c rollup.config.dist.js",
138142
"build:dist:prod": "cross-env NODE_ENV=production rollup -c rollup.config.dist.js",
139143
"build:dist": "npm run build:dist:dev && npm run build:dist:prod",

src/components/message.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
import * as React from 'react';
88
import withIntl from './injectIntl';
9-
import IntlMessageFormat from 'intl-messageformat';
10-
import memoizeIntlConstructor from 'intl-format-cache';
119
import {MessageDescriptor, IntlShape} from '../types';
1210
import * as shallowEquals_ from 'shallow-equal/objects';
1311
const shallowEquals = shallowEquals_;
1412
import {formatMessage as baseFormatMessage} from '../format';
15-
import {invariantIntlContext, DEFAULT_INTL_CONFIG} from '../utils';
13+
import {
14+
invariantIntlContext,
15+
DEFAULT_INTL_CONFIG,
16+
createDefaultFormatters,
17+
} from '../utils';
1618

1719
const defaultFormatMessage: IntlShape['formatMessage'] = (
1820
descriptor,
@@ -29,7 +31,7 @@ const defaultFormatMessage: IntlShape['formatMessage'] = (
2931
...DEFAULT_INTL_CONFIG,
3032
locale: 'en',
3133
},
32-
{getMessageFormat: memoizeIntlConstructor(IntlMessageFormat)},
34+
createDefaultFormatters(),
3335
descriptor,
3436
values
3537
);

src/components/provider.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
createError,
1818
filterProps,
1919
DEFAULT_INTL_CONFIG,
20-
DEFAULT_FORMATTERS,
20+
createDefaultFormatters,
2121
} from '../utils';
2222
import {IntlConfig, IntlShape, IntlFormatters} from '../types';
2323
import {formatters} from '../format';
@@ -141,7 +141,7 @@ class IntlProvider extends React.PureComponent<ResolvedProps, State> {
141141
// `<IntlProvider>`, then its formatters will be used. Otherwise, this
142142
// memoize the `Intl*` constructors and cache them for the lifecycle of
143143
// this IntlProvider instance.
144-
const {formatters = DEFAULT_FORMATTERS} = intlContext || {};
144+
const {formatters = createDefaultFormatters()} = intlContext || {};
145145

146146
this.state = {
147147
context: {
@@ -181,9 +181,7 @@ class IntlProvider extends React.PureComponent<ResolvedProps, State> {
181181

182182
render() {
183183
return (
184-
<Provider value={this.state.context}>
185-
{React.Children.only(this.props.children)}
186-
</Provider>
184+
<Provider value={this.state.context}>{this.props.children}</Provider>
187185
);
188186
}
189187
}

src/format.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export function formatMessage(
249249
| 'defaultFormats'
250250
| 'onError'
251251
>,
252-
state: {getMessageFormat: Formatters['getMessageFormat']},
252+
state: Formatters,
253253
messageDescriptor: MessageDescriptor = {id: ''},
254254
values: Record<string, any> = {}
255255
): string {
@@ -280,7 +280,9 @@ export function formatMessage(
280280

281281
if (message) {
282282
try {
283-
let formatter = state.getMessageFormat(message, locale, formats);
283+
let formatter = state.getMessageFormat(message, locale, formats, {
284+
formatters: state,
285+
});
284286

285287
formattedMessage = formatter.format(values);
286288
} catch (e) {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface Formatters {
7777
): Intl.NumberFormat;
7878
getMessageFormat(
7979
...args: ConstructorParameters<typeof IntlMessageFormat>
80-
): typeof IntlMessageFormat;
80+
): IntlMessageFormat;
8181
getRelativeTimeFormat(
8282
...args: ConstructorParameters<typeof IntlRelativeTimeFormat>
8383
): IntlRelativeTimeFormat;

src/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ export const DEFAULT_INTL_CONFIG: Pick<
105105
onError: defaultErrorHandler,
106106
};
107107

108-
export const DEFAULT_FORMATTERS = {
109-
getDateTimeFormat: memoizeIntlConstructor(Intl.DateTimeFormat),
110-
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
111-
getMessageFormat: memoizeIntlConstructor(IntlMessageFormat),
112-
getRelativeTimeFormat: memoizeIntlConstructor(Intl.RelativeTimeFormat),
113-
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
114-
};
108+
export function createDefaultFormatters() {
109+
return {
110+
getDateTimeFormat: memoizeIntlConstructor(Intl.DateTimeFormat),
111+
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
112+
getMessageFormat: memoizeIntlConstructor(IntlMessageFormat),
113+
getRelativeTimeFormat: memoizeIntlConstructor(Intl.RelativeTimeFormat),
114+
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
115+
};
116+
}

0 commit comments

Comments
 (0)