In JS the RegExp objects are saved globally [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/FormAutofillHeuristics.sys.mjs#89) and therefore persisted across runs. They are initialized lazily [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/HeuristicsRegExp.sys.mjs#647-673) by overriding the get() property on the first access, then un-overriding it. So the JS code looks correct to me in terms of caching. For each `FormAutofillChild._doIdentifyAutofillFields()` (which is called when an input element is focused first after page load), we end up calling `testRegex()` a few times per regexp, twice where the input string `hasLatin1Chars()`, and once where it does not. So on the first iteration we compile the regex twice to byte code. For the next 2 and a bit iterations we reuse the cached byte code. Then we eventually decide the regexp is hot and [decide to tier up](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#618) to jit code. Which means we must recompile RegExp objects are created when the FormAutofillHeurstics. In between each iteration [`discardJitCode`](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#584) is called. So once we have tiered up to JIT code, we now must recompile each regexp each iteration. So perhaps we want to be less eager to call `discardJitCode()`
Bug 1851862 Comment 1 Edit History
Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.
In JS the RegExp objects are saved globally [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/FormAutofillHeuristics.sys.mjs#89) and therefore persisted across runs. They are initialized lazily [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/HeuristicsRegExp.sys.mjs#647-673) by overriding the get() property on the first access, then un-overriding it. So the JS code looks correct to me in terms of caching. For each `FormAutofillChild._doIdentifyAutofillFields()` (which is called when an input element is focused first after page load), we end up calling `testRegex()` a few times per regexp, twice where the input string `hasLatin1Chars()`, and once where it does not. So on the first iteration we compile the regex twice to byte code. For the next 2 and a bit iterations we reuse the cached byte code. Then we eventually decide the regexp is hot and [decide to tier up](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#618) to jit code. Which means we must recompile. In between each iteration [`discardJitCode`](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#584) is called. So once we have tiered up to JIT code, we now must recompile each regexp each iteration. So perhaps we want to be less eager to call `discardJitCode()`
In JS the RegExp objects are saved globally [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/FormAutofillHeuristics.sys.mjs#89) and therefore persisted across runs. They are initialized lazily [here](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/toolkit/components/formautofill/shared/HeuristicsRegExp.sys.mjs#647-673) by overriding the get() property on the first access, then un-overriding it. So the JS code looks correct to me in terms of caching. For each `FormAutofillChild._doIdentifyAutofillFields()` (which is called when an input element is focused first after page load), we end up calling `testRegex()` a few times per regexp, twice where the input string `hasLatin1Chars()`, and once where it does not. So on the first iteration we compile the regex twice to byte code. For the next 2 and a bit iterations we reuse the cached byte code. Then we eventually decide the regexp is hot and [decide to tier up](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#618) to jit code. Which means we must recompile. In between each iteration [`discardJitCode()`](https://searchfox.org/mozilla-central/rev/64eef6b342bca86930caf3ba403c0f8ce621b474/js/src/vm/RegExpObject.cpp#584) is called. So once we have tiered up to JIT code, we now must recompile each regexp each iteration. So perhaps we want to be less eager to call `discardJitCode()`