Registry / devops / regex-cache

regex-cache

JSON →
library0.4.4jsnpmunverified

Memoize the results of calls to the RegExp constructor, avoiding repetitive runtime compilation of the same regex string and options. Version 0.4.4 (last released 2016) is stable but unmaintained; no updates in 8+ years. It caches based on stringified arguments, providing performance improvements when options are few and primitive, but not recommended for modern Node.js as V8's RegExp caching has improved significantly.

npm install regex-cache
INSTALL
IMPORT
SIG · REGEX-CACHE
R
regex-cache
devopsjavascriptv0.4.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const cache = require('regex-cache');
import cache from 'regex-cache';
This package only provides CommonJS (require). ESM import may fail in Node unless using a bundler.
default
import cache from 'regex-cache';
import { cache } from 'regex-cache';
Package has a default export, not a named export. Use default import syntax.
cached function
function createRegex(str) { return new RegExp(str); } const cached = cache(createRegex);
const result = cache(new RegExp('foo'));
cache() expects a function that returns a RegExp, not a RegExp object itself.

Shows how to wrap a regex-creation function with cache() and benchmark the performance difference.

const cache = require('regex-cache'); function makeRegex(pattern, flags) { return new RegExp(pattern, flags); } const cachedMakeRegex = cache(makeRegex); // Use cached version console.log(cachedMakeRegex('\\d+', 'g').test('123')); // true console.log(cachedMakeRegex('\\d+', 'g') === cachedMakeRegex('\\d+', 'g')); // true (same cached regex) // Benchmark comparison const { performance } = require('perf_hooks'); const start = performance.now(); for (let i = 0; i < 10000; i++) { new RegExp('test', 'i'); } const without = performance.now() - start; const startCached = performance.now(); for (let i = 0; i < 10000; i++) { cachedMakeRegex('test', 'i'); } const with = performance.now() - startCached; console.log(`Without cache: ${without.toFixed(2)}ms`); console.log(`With cache: ${with.toFixed(2)}ms`);
Debug
Known issues
deprecatedPackage is unmaintained since 2016 and may have compatibility issues with modern Node.js (e.g., V8 RegExp improvements).
fix
Consider using native RegExp caching or a modern alternative like p-memoize.
affects: >=0.4.4
gotchaThe cache uses JSON.stringify for arguments, so non-primitive options (objects) will cause stringification differences and hurt performance.
fix
Only use with primitive arguments or override the comparator (not supported by the API).
affects: >=0.1.0
gotchaThe cache object grows unboundedly; no TTL or LRU eviction, which may lead to memory leaks in long-running processes.
fix
Manually clear the cache or use a library with built-in eviction like lru-cache.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: regexCache is not a function
Using named import instead of default import.
fix
Use const cache = require('regex-cache'); or import cache from 'regex-cache'; (if bundling).
Cannot find module 'regex-cache'
Package not installed or used in an ESM context without proper resolution.
fix
Run npm install regex-cache and use require() (CommonJS) or ensure your bundler handles CJS.
RangeError: Maximum call stack size exceeded
Passing a function that calls cache recursively without a base case.
fix
Ensure the wrapped function does not invoke cache again on itself.
Upgrade
Version history
0.4.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
regex-cache — npm install regex-cache · libregistry