Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.
npm install regex-cache> Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.
Install with npm:
``sh`
$ npm install --save regex-cache
* Read what this does.
* See the benchmarks
Wrap a function like this:
`js`
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:
`js
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);
`
* 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.
* A few options are passed, and the values are primitives. The limited benchmarks I did show that caching is beneficial when up to 8 or 9 options are passed.
* The values of options are not primitives. When non-primitives must be compared for equality, the time to compare the options is most likely as long or longer than the time to just create a new regex.
Performance results, with and without regex-cache:
`bashno args passed (defaults)
with-cache x 8,699,231 ops/sec ±0.86% (93 runs sampled)
without-cache x 2,777,551 ops/sec ±0.63% (95 runs sampled)
#
#
Run benchmarks
Install dev dependencies:
`bash
npm i -d && npm run benchmarks
`What this 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.About
$3
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
$3
| Commits | Contributor |
| --- | --- |
| 31 | jonschlinkert |
| 1 | MartinKolarik |
$3
_(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)_
To generate the readme, run the following command:
`sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
`$3
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
`sh
$ npm install && npm test
``Jon Schlinkert
* github/jonschlinkert
* twitter/jonschlinkert
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
*
_This file was generated by verb-generate-readme, v0.6.0, on September 01, 2017._