Skip to content

perf: cache compiled regex patterns on braspagCards to avoid per-keyup RegExp construction#120

Draft
Copilot wants to merge 2 commits intohotfix/error-recognition-flagfrom
copilot/sub-pr-119
Draft

perf: cache compiled regex patterns on braspagCards to avoid per-keyup RegExp construction#120
Copilot wants to merge 2 commits intohotfix/error-recognition-flagfrom
copilot/sub-pr-119

Conversation

Copy link

Copilot AI commented Mar 2, 2026

getCardInfoFromNumber is invoked on every keyup event, causing new RegExp(...) to be called repeatedly for each card's regex_include and regex_exclude patterns — an unnecessary overhead given the patterns never change.

Changes

  • Pre-compile regexes once — added a forEach pass over braspagCards immediately after the array definition, storing compiled patterns as _regex_include / _regex_exclude (null when the source string is empty):
braspagCards.forEach(function (card) {
    card._regex_include = card.regex_include ? new RegExp(card.regex_include) : null;
    card._regex_exclude = card.regex_exclude ? new RegExp(card.regex_exclude) : null;
});
  • getCardInfoFromNumber — replaced inline new RegExp(card.regex_include/exclude) calls with direct use of the cached _regex_include / _regex_exclude properties, checking against null instead of != ''.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ated compilation on keyup

Co-authored-by: willian-hf-rodrigues <140353340+willian-hf-rodrigues@users.noreply.github.com>
Copilot AI changed the title [WIP] Update regex recognition for credit and debit card handling perf: cache compiled regex patterns on braspagCards to avoid per-keyup RegExp construction Mar 2, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces per-keystroke overhead in the checkout card-brand detection by compiling card include/exclude regex patterns once and reusing them inside getCardInfoFromNumber.

Changes:

  • Pre-compiles regex_include / regex_exclude into cached _regex_include / _regex_exclude RegExp objects on script load.
  • Updates getCardInfoFromNumber to use the cached regex objects (and null checks) instead of constructing new RegExp instances on each keyup.
Comments suppressed due to low confidence (2)

assets/js/braspag.js:153

  • Regex pre-compilation happens at script load time; if any regex_include / regex_exclude string is invalid, new RegExp(...) will throw and prevent the entire checkout JS from initializing. Consider wrapping the compilation in a try/catch (and falling back to null + optionally logging) so a single bad pattern doesn’t hard-fail the page.
braspagCards.forEach(function (card) {
	card._regex_include = card.regex_include ? new RegExp(card.regex_include) : null;
	card._regex_exclude = card.regex_exclude ? new RegExp(card.regex_exclude) : null;

assets/js/braspag.js:193

  • _regex_exclude is only consulted inside the cardTypeFound block (i.e., only when _regex_include is present and matches). Cards configured with an exclude but no include will never have their exclude applied (e.g., the Discover entry defines regex_exclude while regex_include is empty). Consider either enforcing “exclude requires include” in the data, or updating the matching logic so exclude can also be applied when the prefix-pattern match succeeds.
			let cardTypeFound = false;
			if (card._regex_include !== null) {
				if (card._regex_include.test(num)) {
					cardTypeFound = true;
				}
			}

			if (cardTypeFound) {

				if (card._regex_exclude === null) {
					return card;
				}

				if (!card._regex_exclude.test(num)) {
					return card;
				}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants