lephyrus/ngx-translate-messageformat-compiler

Performance Problem

g1tc4t opened this issue · 3 comments

g1tc4t commented

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;
      },
      {}
    );
  }

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 #111

@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.

Fixed by #112.