Two includes() checks made i18next up to 5x faster on long text. This post shows that fix and two more.
A regular expression is a good tool, but it is not free. Each call scans the input, and a bad pattern can scan the same characters many times.
Three of my performance PRs had the same idea: do a cheap check before the expensive regex work . Maintainers merged all three. This post shows each change, the numbers, and the rule you can use in your own code.
1. i18next: check the prefix before you run the regex
i18next (35M npm downloads a week) runs its interpolation regex and its nesting regex on every translated text. The regexes look for {{name}} and $t(key) .
Most texts have neither. "Save", "Cancel" and "Welcome back" contain no {{ and no $t( . But i18next ran both regexes on them anyway, and each scan read the full text to find nothing.
Every match of these regexes starts with a fixed prefix. So a text without the prefix cannot match.…