Skip to content

Cache compiled emoji regexes in countEmoji#4

Merged
cybermejo merged 1 commit into
worktree-studio-modularize-and-testfrom
worktree-studio-countemoji-cache
Jun 17, 2026
Merged

Cache compiled emoji regexes in countEmoji#4
cybermejo merged 1 commit into
worktree-studio-modularize-and-testfrom
worktree-studio-countemoji-cache

Conversation

@cybermejo

Copy link
Copy Markdown
Owner

Summary

Caches the compiled emoji regexes in countEmoji so it stops recompiling ~70 RegExps on every call.

Stacked on #2 (base worktree-studio-modularize-and-test) — countEmoji lives in src/lib/analysis.js, which is introduced by #2. GitHub will retarget this to main once #2 merges.

Why

countEmoji built a fresh RegExp per 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

  • 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 exactly once total.
  • Counting semantics are unchanged: same per-emoji regex and g flag. String.prototype.match with /g is stateless (it ignores and resets lastIndex), 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.

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 cybermejo self-assigned this Jun 17, 2026
@cybermejo cybermejo added the enhancement New feature or request label Jun 17, 2026
@cybermejo
cybermejo merged commit f2c5782 into worktree-studio-modularize-and-test Jun 17, 2026
4 checks passed
@cybermejo
cybermejo deleted the worktree-studio-countemoji-cache branch June 17, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant