Cache compiled emoji regexes in countEmoji#4
Merged
cybermejo merged 1 commit intoJun 17, 2026
Merged
Conversation
countEmoji recompiled one RegExp per emoji (~70) on every call, and it runs once per tweet across several call sites (enrich, analyzeSentiment, analyzeTone) — so a large CSV recompiled hundreds of thousands of throwaway regexes. Compile each list's regexes once and cache them in a WeakMap keyed on the list array (POSITIVE_EMOJI / NEGATIVE_EMOJI are stable module constants, so they compile once total). Counting semantics are unchanged: same per-emoji regex + 'g' flag, and String.prototype.match with /g is stateless (resets lastIndex), so reusing the compiled regexes across calls is safe. Adds a repeated-call regression test. Build + 41 tests green.
cybermejo
merged commit Jun 17, 2026
f2c5782
into
worktree-studio-modularize-and-test
4 checks passed
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.
Summary
Caches the compiled emoji regexes in
countEmojiso it stops recompiling ~70RegExps on every call.Why
countEmojibuilt a freshRegExpper emoji on each invocation, and it runs once per tweet across several call sites (enrich,analyzeSentiment,analyzeTone). On a large CSV that's hundreds of thousands of throwaway regex compilations.What changed
WeakMapkeyed on the list array.POSITIVE_EMOJI/NEGATIVE_EMOJIare stable module constants, so they compile exactly once total.gflag.String.prototype.matchwith/gis stateless (it ignores and resetslastIndex), so reusing the compiled regexes across calls is safe.Testing
Adds a repeated-call regression test (guards against any stateful-regex bug). Build green (identical CSS hash) and 41 tests pass.