alibaba / react-intl-universal

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

cache #232

Open dvlin-dev opened 10 months ago

dvlin-dev commented 10 months ago

Adding cache is necessary to avoid redundant processing of the same values through IntlMessageFormat, which significantly improves performance in scenarios with long lists.

dvlin-dev commented 10 months ago

I added some code to see the performance improvement after caching, which is about 10 times faster.

test("cache", () => {
  const key = "HELLO";
  const variables = { name: 'World' };
  const currentLocale = "en-US"
  const cacheKey = key + JSON.stringify(variables) + currentLocale;

  intl.init({ locales, currentLocale });

  + console.time('Before caching')
  expect(intl.get(key, variables)).toBe("Hello, World");
  + console.timeEnd('Before caching')

  + console.time('After caching')
  expect(intl.get(key, variables)).toBe("Hello, World");
  + console.timeEnd('After caching')
});

image