lephyrus / ngx-translate-messageformat-compiler

Advanced pluralization (and more) for ngx-translate, using standard ICU syntax which is compiled with the help of messageformat.js.
MIT License
93 stars 29 forks source link

Performance Problem #110

Closed g1tc4t closed 8 months ago

g1tc4t commented 1 year ago

When loading a large translation file (a few thousand entries) the browser needs a few seconds to parse the entries. Doing some performance investigations I found that the spread operator used in translate-message-format-compiler.ts leads to O(n)=n²

This can be fixed to be O(n)=n quite easily:

  public compileTranslations(translations: any, lang: string): any {
    if (typeof translations === "string") {
      return this.compile(translations, lang);
    }

    return Object.keys(translations).reduce<{ [key: string]: any }>(
      (acc, key) => {
        const value = translations[key];
        // just assign the value; no object initialization and no data copying needed
        acc[key] = this.compileTranslations(value, lang);
        return acc;
      },
      {}
    );
  }
Endrzei commented 11 months ago

We've also encountered the issue and confirmed this resolves the problem. Thx for the investigation @g1tc4t, I've opened a pr with your fix https://github.com/lephyrus/ngx-translate-messageformat-compiler/pull/111

lephyrus commented 11 months ago

@g1tc4t So little code and I've managed to introduce a function that doesn't scale well at all. 🤦 Thanks for the investigation and verifying a fix.

@Endrzei Thanks for the PR.

lephyrus commented 8 months ago

Fixed by #112.