Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Intl instances to improve performance #894

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- cache Intl instances to improve performance [#894](https://github.com/CartoDB/carto-react/pull/894)
- Support for `onRowMouseEnter` and `onRowMouseLeave` handlers for Table Widget [#907](https://github.com/CartoDB/carto-react/pull/907)

## 3.0.0
Expand Down
50 changes: 34 additions & 16 deletions packages/react-ui/src/hooks/useImperativeIntl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,40 @@ import {
} from '../localization/localeUtils';

const cache = createIntlCache();
const intlInstanceCache = new WeakMap();

export default function useImperativeIntl(intlConfig) {
return useMemo(() => {
const locale = intlConfig?.locale || DEFAULT_LOCALE;
const messagesLocale = findMatchingMessagesLocale(locale, messages);
const intMessages = {
...(messages[messagesLocale] || {}),
...(intlConfig?.messages || {})
};
const createIntlInstance = (intlConfig) => {
const locale = intlConfig?.locale || DEFAULT_LOCALE;
const messagesLocale = findMatchingMessagesLocale(locale, messages);
const intMessages = {
...(messages[messagesLocale] || {}),
...(intlConfig?.messages || {})
};

const combinedMessages = flattenMessages(intMessages);
return createIntl(
{
locale,
messages: combinedMessages
},
cache
);
};

return createIntl(
{
locale,
messages: flattenMessages(intMessages)
},
cache
);
}, [intlConfig]);
const getGloballyCachedIntl = (intlConfig) => {
// This is very simple cache exploits fact that Intl instance is actually same for most of time
// so we can reuse those maps across several instances of same components
// note, useMemo can't cache accross many that globally and flattenMessages over _app_ and c4r messages is quite costly
// and would be paid for every c4r component mounted.
let cachedInstance = intlInstanceCache.get(intlConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let cachedInstance = intlInstanceCache.get(intlConfig);
const cachedInstance = intlInstanceCache.get(intlConfig);

if (cachedInstance) {
return cachedInstance;
}
const newInstance = createIntlInstance(intlConfig);
intlInstanceCache.set(intlConfig, newInstance);
return newInstance;
};

export default function useImperativeIntl(intlConfig) {
return getGloballyCachedIntl(intlConfig);
}
Loading