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
Draft
perf: cache compiled regex patterns on braspagCards to avoid per-keyup RegExp construction#120Copilot wants to merge 2 commits intohotfix/error-recognition-flagfrom
Copilot wants to merge 2 commits intohotfix/error-recognition-flagfrom
Conversation
…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
There was a problem hiding this comment.
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_excludeinto cached_regex_include/_regex_excludeRegExp objects on script load. - Updates
getCardInfoFromNumberto use the cached regex objects (andnullchecks) instead of constructing new RegExp instances on eachkeyup.
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_excludestring is invalid,new RegExp(...)will throw and prevent the entire checkout JS from initializing. Consider wrapping the compilation in atry/catch(and falling back tonull+ 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_excludeis only consulted inside thecardTypeFoundblock (i.e., only when_regex_includeis present and matches). Cards configured with an exclude but no include will never have their exclude applied (e.g., the Discover entry definesregex_excludewhileregex_includeis empty). Consider either enforcing “exclude requires include” in the data, or updating the matching logic soexcludecan 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.
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.
getCardInfoFromNumberis invoked on everykeyupevent, causingnew RegExp(...)to be called repeatedly for each card'sregex_includeandregex_excludepatterns — an unnecessary overhead given the patterns never change.Changes
forEachpass overbraspagCardsimmediately after the array definition, storing compiled patterns as_regex_include/_regex_exclude(nullwhen the source string is empty):getCardInfoFromNumber— replaced inlinenew RegExp(card.regex_include/exclude)calls with direct use of the cached_regex_include/_regex_excludeproperties, checking againstnullinstead 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.