Package Exports
- regex-cache
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (regex-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
regex-cache

Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.
Read what this does.
Install with npm
npm i regex-cache --save
Usage
Wrap a function like this:
var cache = require('regex-cache');
var someRegex = cache(require('some-regex-lib'));
Caching a regex
If you want to cache a regex after calling new RegExp()
, or you're requiring a module that returns a regex, wrap it with a function first:
var cache = require('regex-cache');
function yourRegex(str, opts) {
// do stuff to str and opts
return new RegExp(str, opts.flags);
}
var regex = cache(yourRegex);
Recommendations
- Use this when no options are passed to the function that creates the regex. Regardless of how big or small the regex is, when zero options are passed, caching will be faster than not.
- Do not use this when you are passing options to create a simple regex. No matter how many options are passed, one or fifty, a simple regex will not benefit from caching.
- However, if the logic for creating the regex is extensive (much more than the logic used in support.js, like with globbing, brace expansion, etc), then it might make sense to use this if options are passed.
Example benchmarks
Performance results for a random regex lib, mentions-regex, with and without regex-cache, and no options passed:
#1: no-args passed, and defaults are used
with-cache.js x 9,141,988 ops/sec ±0.61% (98 runs sampled)
without-cache.js x 2,818,715 ops/sec ±0.48% (99 runs sampled)
#2: a string is passed
with-cache.js x 7,479,890 ops/sec ±0.66% (95 runs sampled)
without-cache.js x 2,123,907 ops/sec ±0.33% (98 runs sampled)
What it does
If you're using new RegExp('foo')
instead of a regex literal, it's probably because you need to dyamically generate a regex based on user options or some other potentially changing factors.
When your function creates a string based on user inputs and passes it to the RegExp
constructor, regex-cache caches the results. The next time the function is called if the key of a cached regex matches the user input (or no input was given), the cached regex is returned, avoiding unnecessary runtime compilation.
Using the RegExp constructor offers a lot of flexibility, but the runtime compilation comes at a price - it's slow. Not specifically because of the call to the RegExp constructor, but because you have to build up the string before new RegExp()
is even called.
Run tests
Install dev dependencies:
npm i -d && npm test
Run benchmarks
Install dev dependencies:
npm i -d && npm run benchmarks
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb on February 17, 2015.