[Translator] Cache the RegExp compiled by strtr() - #3784
Open
Kocal wants to merge 1 commit into
Open
Conversation
Contributor
📊 Packages dist files size differenceThanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
|
|||||||||
Kocal
force-pushed
the
perf/translator-strtr-regex-cache
branch
from
August 15, 2026 22:10
628fa51 to
9533ce1
Compare
| Q | A
| -------------- | ---
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Documentation? | no
| Issues | -
| License | MIT
`strtr()` built and compiled a new `RegExp` on every call, and it is
called once per `trans()` with parameters. The pattern only depends on the
parameter names, which come from a fixed set in the source code.
Cache the compiled `RegExp` by pattern and hoist the escaping regex out of
the mapping callback.
200k `trans()` calls: ~226 ms -> ~195 ms with parameters, ~214 ms -> ~189 ms
with plural forms.
Browser-side JavaScript, so there is no Blackfire profile for this one.
Benchmarked on node 22, after `pnpm --filter @symfony/ux-translator run
build`, from the repository root with `node bench.mjs`:
```js
import { createTranslator } from './src/Translator/assets/dist/translator_controller.js';
const messages = {
'app.greeting': { id: 'app.greeting', translations: { messages: { en: 'Hello %name%, you have %count% new messages' } } },
'app.plural': { id: 'app.plural', translations: { messages: { en: '{0} No apples|one: One apple|more: %count% apples' } } },
};
const translator = createTranslator({ messages, locale: 'en' });
const n = 200000;
let start = process.hrtime.bigint();
for (let i = 0; i < n; i++) {
translator.trans('app.greeting', { '%name%': 'Hugo', '%count%': i });
}
console.log(`simple ${n} trans() -> ${Number(process.hrtime.bigint() - start) / 1e6} ms`);
start = process.hrtime.bigint();
for (let i = 0; i < n; i++) {
translator.trans('app.plural', { '%count%': i % 7 });
}
console.log(`plural ${n} trans() -> ${Number(process.hrtime.bigint() - start) / 1e6} ms`);
```
Analysis, implementation and benchmarks by Claude Opus 5.
Kocal
force-pushed
the
perf/translator-strtr-regex-cache
branch
from
August 15, 2026 22:21
9533ce1 to
d2f272a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
strtr()built and compiled a newRegExpon every call, and it is called once pertrans()with parameters. The pattern only depends on the parameter names, which come from a fixed set in the source code.Cache the compiled
RegExpby pattern and hoist the escaping regex out of the mapping callback.200k
trans()calls: ~226 ms -> ~195 ms with parameters, ~214 ms -> ~189 ms with plural forms.Browser-side JavaScript, so there is no Blackfire profile for this one. Benchmarked on node 22, after
pnpm --filter @symfony/ux-translator run build, from the repository root withnode bench.mjs:Analysis, implementation and benchmarks by Claude Opus 5.