Skip to content

Fix #164: Runtime performance improvements#165

Merged
JasonPierce merged 2 commits intomainfrom
jp-issue-164
Mar 24, 2026
Merged

Fix #164: Runtime performance improvements#165
JasonPierce merged 2 commits intomainfrom
jp-issue-164

Conversation

@JasonPierce
Copy link
Contributor

Fixes #164

  • replaceProfanity — O(n) instead of O(n*m): Replaced repeated slice + concat (each creating a full copy of the result string) with an array of parts joined once at the end. Also eliminated the offset tracking since we no longer mutate a running string.
  • Vowel regexes hoisted to module-level constants: FIRST_VOWEL_RE and ALL_VOWELS_RE are created once instead of new RegExp(...) on every matched profane word during censoring.
  • Word-boundary regexes hoisted to module-level constants: UNICODE_WORD_CHAR_RE and ASCII_WORD_CHAR_RE replace the isWordChar closure that was recreated on every isWhitelisted call, with a new regex instance per invocation.
  • getRegex reduced allocations: Replaced [...new Set(languages.map(...))] + .toSorted() + .flatMap() + .filter() chain with a single-pass dedup loop, in-place sort, and a simple for...of with push — fewer intermediate arrays and closures.

Hoist vowel and word-boundary regexes to module-level constants, replace
O(n·m) string slicing in replaceProfanity with array-join, and reduce
intermediate allocations in getRegex.
@JasonPierce JasonPierce merged commit c120ad8 into main Mar 24, 2026
5 checks passed
@JasonPierce JasonPierce deleted the jp-issue-164 branch March 24, 2026 01:19
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.

Runtime performance improvements

1 participant