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 --saveUsage
When a regex is created using the new RegExp() constructor and a function is returned, you can wrap the function like this:
var cache = require('regex-cache');
var someRegex = cache(require('some-regex-lib'));If the code you're wrapping returns a regex (e.g. not a function), wrap it first so it returns a function to achieve the same results. (Note that after wrapping the function with regex-cache a regexp should be returned instead of a function).
Recommendations
DO use this when:
- you're not passing options (regardless of how big or small the regex is, caching makes it faster)
- you are passing options and the logic for creating the regex is extensive (like with globbing, brace expansion, etc)
DO NOT use this when:
- you are passing options to create a simple regex
Example benchmarks
Performance results for a random regex lib, mentions-regex, with and without regex-cache, and no options passed:
with regex-cache x 11,256,663 ops/sec ±0.59% (97 runs sampled)
without regex-cache x 1,812,916 ops/sec ±1.66% (24 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.
regex-cache is a simple way to cache the results of a previous call to the RegExp constructor to avoid unnecessary runtime compilation when both the string and options being passed have not changed.
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 to create the regex based on various inputs.
Run tests
Install dev dependencies:
npm i -d && npm testRun benchmarks
Install dev dependencies:
npm i -d && npm run benchmarksContributing
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 09, 2015.