Skip to content

determineLocale / requiresTranslation are slow with many locales #2067

Description

@YahiaMH

We recently migrated our app to gt-react (11.1.6) with 55 locales in our gt.config.json, and noticed that re-renders were roughly 2x slower than before the migration. After profiling, most of the overhead was coming from the locale utilities rather than anything in our own code.

The cause seems to be that LocaleConfig.determineLocale() and requiresTranslation() re-validate the entire locale list on every call, and both of them run per hook per render through useLocale/useGT. With a big locale list that adds up fast:

const { LocaleConfig } = require('@generaltranslation/format');

const locales = ["es","zh","zh-Hant","hi","bn","ar","pt-BR","fr","de","it","ru","pt-PT","ja","ko","vi","th","id","ms","fil","ur","ta","ml","gu","fa","tr","kk","mn","hy","ka","uk","pl","cs","sk","sr","hr","sl","mk","bg","lt","et","lv","sv","no","da","fi","is", "nl","el","hu","ro","sq","ca","cy","af"];
const config = new LocaleConfig({ defaultLocale: 'en', locales });

console.time('x1000');
for (let i = 0; i < 1000; i++) config.requiresTranslation('ar');
console.timeEnd('x1000'); // ~150ms

Since the config fields these read from are only set in the constructor, we memoized these methods in a local patch as a workaround. The outputs are unchanged and the same 1000-call loop drops to ~9ms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions