js-hoist-regexp

Hoist RegExp creation to module scope or memoize with useMemo.

2.0k|117|Updated Mar 27, 2025
One-click install
npx skills add https://github.com/TheOrcDev/8bitcn-ui --skill js-hoist-regexp
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: js-hoist-regexp
Source: https://github.com/TheOrcDev/8bitcn-ui/tree/main/.claude/skills/js-hoist-regexp
Command: npx skills add https://github.com/TheOrcDev/8bitcn-ui --skill js-hoist-regexp

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers avoid costly RegExp creation inside render by hoisting to module scope or memoizing with useMemo, resulting in more predictable and efficient React components.

Core Features & Use Cases

  • Hoist RegExp creation to module scope to reuse a single instance.
  • Memoize dynamic RegExp with useMemo to recompute only when inputs change.
  • Avoid lastIndex-related bugs by not using global /g patterns inside frequently rendered code.

Quick Start

Start by placing a reusable RegExp at module scope or wrapping its creation in useMemo in your React components and utilities.

Frequently Asked Questions about js-hoist-regexp

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I stop React from recreating RegExp on every render?

Hoist RegExp creation to module scope to reuse a single instance across renders, or wrap dynamic RegExp with useMemo to recompute only when inputs change. This eliminates costly object allocation on every render cycle.

What's the best way to use regex patterns in React components without performance issues?

Place reusable regex at module scope for static patterns, or memoize with useMemo for dynamic ones. Stable regex initialization prevents unnecessary re-creation and ensures predictable, efficient pattern matching.

Why do global regex patterns with /g cause bugs in frequently rendered code?

Global patterns maintain lastIndex state between matches, causing unpredictable behavior when the same regex is reused across render cycles. Hoisting or memoizing regex avoids this state-related pitfall.

Can I use memoization to optimize text processing in React utilities?

Yes. Wrap RegExp creation in useMemo within utilities and components that process text or perform frequent pattern matches, ensuring the regex is recomputed only when dependencies change.

When should I hoist RegExp to module scope versus using useMemo?

Hoist static patterns to module scope for simplicity and zero overhead. Use useMemo when the regex depends on component props or state that changes over time.