alibaba / react-intl-universal

Internationalize React apps. Not only for Component but also for Vanilla JS.
1.33k stars 153 forks source link

Performance problems of get method #224

Closed dvlin-dev closed 11 months ago

dvlin-dev commented 12 months ago

packages/react-intl-universal/src/ReactIntlUniversal.js

try {
const msgFormatter = new IntlMessageFormat(msg, currentLocale, formats);
return msgFormatter.format(variables);
} catch (err) {
this.options.warningHandler(
`react-intl-universal format message failed for key='${key}'.`,
err.message
);
return msg;
}

No matter whether the get method passes variables or not, it will always take the IntlMessageFormat method. I don't think this is necessary. If there is no variables parameter, it can return msg directly, IntlMessageFormat will cause additional performance consumption.

If you don't pass variables, I think you can return msg directly, for example:

if (variables) {
      ...
+   } else {
+      return msg;
   }

    try {
      const msgFormatter = new IntlMessageFormat(msg, currentLocale, formats);
      return msgFormatter.format(variables);
    } catch (err) {
      this.options.warningHandler(
        `react-intl-universal format message failed for key='${key}'.`,
        err.message
      );
      return msg;
    }
dvlin-dev commented 12 months ago

In the actual project has encountered serious performance problems, currently using patch-package to hit patch to solve, It would be great if we could merge pr.

cwtuan commented 11 months ago

fixed in react-intl-universal@2.7.0