regex-cache
Advanced tools
Comparing version 0.3.0 to 0.4.1
41
index.js
@@ -10,3 +10,4 @@ /*! | ||
var toKey = require('to-key'); | ||
var isPrimitive = require('is-primitive'); | ||
var equal = require('is-equal-shallow'); | ||
@@ -29,22 +30,35 @@ /** | ||
function regexCache(fn, str, options) { | ||
var key = '_default_'; | ||
function regexCache(fn, str, opts) { | ||
var key = '_default_', regex, cached; | ||
if (!str) { | ||
return cache[key] || (cache[key] = fn()); | ||
if (!str && !opts) { | ||
if (typeof fn !== 'function') { | ||
return fn; | ||
} | ||
return basic[key] || (basic[key] = fn()); | ||
} | ||
if (!options) { | ||
if (typeof str === 'string') { | ||
return cache[str] || (cache[str] = fn(str)); | ||
} else { | ||
key = toKey(str); | ||
return cache[key] || (cache[key] = fn(str)); | ||
var isString = typeof str === 'string'; | ||
if (isString) { | ||
if (!opts) { | ||
return basic[str] || (basic[str] = fn(str)); | ||
} | ||
key = str; | ||
} else { | ||
opts = str; | ||
} | ||
key = str + toKey(options); | ||
return cache[key] || (cache[key] = fn(str, options)); | ||
cached = cache[key]; | ||
if (cached && equal(cached.opts, opts)) { | ||
return cached.regex; | ||
} | ||
memo(key, opts, (regex = fn(str, opts))); | ||
return regex; | ||
} | ||
function memo(key, opts, regex) { | ||
cache[key] = {regex: regex, opts: opts}; | ||
} | ||
/** | ||
@@ -55,1 +69,2 @@ * Expose `cache` | ||
var cache = module.exports.cache = {}; | ||
var basic = module.exports.basic = {}; |
{ | ||
"name": "regex-cache", | ||
"description": "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.", | ||
"version": "0.3.0", | ||
"version": "0.4.1", | ||
"homepage": "https://github.com/jonschlinkert/regex-cache", | ||
@@ -33,8 +33,9 @@ "author": { | ||
"dependencies": { | ||
"benchmarked": "^0.1.3", | ||
"chalk": "^0.5.1", | ||
"micromatch": "^1.2.2", | ||
"to-key": "^1.0.0" | ||
"is-equal-shallow": "^0.1.1", | ||
"is-primitive": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"benchmarked": "^0.1.4", | ||
"chalk": "^1.0.0", | ||
"micromatch": "^2.1.0", | ||
"mocha": "^2.1.0", | ||
@@ -41,0 +42,0 @@ "should": "*" |
# regex-cache [![NPM version](https://badge.fury.io/js/regex-cache.svg)](http://badge.fury.io/js/regex-cache) [![Build Status](https://travis-ci.org/jonschlinkert/regex-cache.svg)](https://travis-ci.org/jonschlinkert/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](#what-this-does). | ||
- Read [what this does](#what-this-does). | ||
- See [the benchmarks](#benchmarks) | ||
@@ -37,24 +39,79 @@ ## Install with [npm](npmjs.org) | ||
### Recommendations | ||
## 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](./support.js), like with globbing, brace expansion, etc), then it might make sense to use this if options are passed. | ||
### 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. | ||
* **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. | ||
### Do not use this when... | ||
* **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. | ||
### Example benchmarks | ||
[Performance results](#benchmarks) for a random regex lib, [mentions-regex], with and without regex-cache, and no options passed: | ||
Performance results, with and without regex-cache: | ||
```bash | ||
#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) | ||
# no 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) | ||
#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) | ||
# string and six options passed | ||
with-cache x 1,885,934 ops/sec ±0.80% (93 runs sampled) | ||
without-cache x 1,256,893 ops/sec ±0.65% (97 runs sampled) | ||
# string only | ||
with-cache x 7,723,256 ops/sec ±0.87% (92 runs sampled) | ||
without-cache x 2,303,060 ops/sec ±0.47% (99 runs sampled) | ||
# one option passed | ||
with-cache x 4,179,877 ops/sec ±0.53% (100 runs sampled) | ||
without-cache x 2,198,422 ops/sec ±0.47% (95 runs sampled) | ||
# two options passed | ||
with-cache x 3,256,222 ops/sec ±0.51% (99 runs sampled) | ||
without-cache x 2,121,401 ops/sec ±0.79% (97 runs sampled) | ||
# six options passed | ||
with-cache x 1,816,018 ops/sec ±1.08% (96 runs sampled) | ||
without-cache x 1,157,176 ops/sec ±0.53% (100 runs sampled) | ||
# | ||
# diminishing returns happen about here | ||
# | ||
# ten options passed | ||
with-cache x 1,210,598 ops/sec ±0.56% (92 runs sampled) | ||
without-cache x 1,665,588 ops/sec ±1.07% (100 runs sampled) | ||
# twelve options passed | ||
with-cache x 1,042,096 ops/sec ±0.68% (92 runs sampled) | ||
without-cache x 1,389,414 ops/sec ±0.68% (97 runs sampled) | ||
# twenty options passed | ||
with-cache x 661,125 ops/sec ±0.80% (93 runs sampled) | ||
without-cache x 1,208,757 ops/sec ±0.65% (97 runs sampled) | ||
# | ||
# when non-primitive values are compared | ||
# | ||
# single value on the options is an object | ||
with-cache x 1,398,313 ops/sec ±1.05% (95 runs sampled) | ||
without-cache x 2,228,281 ops/sec ±0.56% (99 runs sampled) | ||
``` | ||
## What it does | ||
## 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. | ||
@@ -75,10 +132,2 @@ | ||
## Run benchmarks | ||
Install dev dependencies: | ||
```bash | ||
npm i -d && npm run benchmarks | ||
``` | ||
## Contributing | ||
@@ -90,6 +139,4 @@ Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/regex-cache/issues) | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
@@ -101,4 +148,4 @@ Copyright (c) 2015 Jon Schlinkert | ||
_This file was generated by [verb](https://github.com/assemble/verb) on February 17, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 25, 2015._ | ||
[mentions-regex]: https://github.com/regexps/mentions-regex |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8367
2
54
148
5
+ Addedis-equal-shallow@^0.1.1
+ Addedis-primitive@^2.0.0
+ Addedis-equal-shallow@0.1.3(transitive)
+ Addedis-primitive@2.0.0(transitive)
- Removedbenchmarked@^0.1.3
- Removedchalk@^0.5.1
- Removedmicromatch@^1.2.2
- Removedto-key@^1.0.0
- Removedansi@0.3.1(transitive)
- Removedansi-regex@0.2.12.1.1(transitive)
- Removedansi-styles@1.1.02.2.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedarr-diff@1.1.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarr-map@1.0.0(transitive)
- Removedarr-union@3.1.0(transitive)
- Removedarray-slice@0.2.3(transitive)
- Removedasync-array-reduce@0.2.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbenchmark@1.0.0(transitive)
- Removedbenchmarked@0.1.5(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbraces@1.8.5(transitive)
- Removedcamel-case@1.2.2(transitive)
- Removedchalk@0.5.11.1.3(transitive)
- Removedclone@1.0.4(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddebug@2.6.9(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@4.0.1(transitive)
- Removedexpand-brackets@0.1.5(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedexpand-tilde@1.2.2(transitive)
- Removedextend-shallow@1.1.42.0.1(transitive)
- Removedextglob@0.2.0(transitive)
- Removedfile-reader@1.1.1(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.4(transitive)
- Removedfor-in@0.1.81.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedfs-exists-sync@0.1.0(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedglob-base@0.1.1(transitive)
- Removedglob-parent@1.3.0(transitive)
- Removedglob-path-regex@1.0.0(transitive)
- Removedglobal-modules@0.2.3(transitive)
- Removedglobal-prefix@0.1.5(transitive)
- Removedhas-ansi@0.1.02.0.0(transitive)
- Removedhas-glob@0.1.1(transitive)
- Removedhas-values@0.1.4(transitive)
- Removedhomedir-polyfill@1.0.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-extendable@0.1.1(transitive)
- Removedis-extglob@1.0.0(transitive)
- Removedis-glob@1.1.32.0.1(transitive)
- Removedis-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-valid-glob@0.3.0(transitive)
- Removedis-windows@0.2.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedisobject@0.2.02.1.0(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedkind-of@1.1.03.2.26.0.3(transitive)
- Removedlazy-cache@1.0.42.0.2(transitive)
- Removedlower-case@1.1.4(transitive)
- Removedmap-files@0.8.2(transitive)
- Removedmatched@0.4.4(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmicromatch@1.6.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedms@2.0.0(transitive)
- Removedobject.omit@0.2.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedparse-glob@2.1.1(transitive)
- Removedparse-passwd@1.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedread-yaml@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedreplace-ext@0.0.1(transitive)
- Removedresolve-dir@0.1.1(transitive)
- Removedsentence-case@1.1.3(transitive)
- Removedset-getter@0.1.1(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstrip-ansi@0.3.03.0.1(transitive)
- Removedsupports-color@0.2.02.0.0(transitive)
- Removedto-key@1.0.0(transitive)
- Removedto-object-path@0.3.0(transitive)
- Removedupper-case@1.1.3(transitive)
- Removedvinyl@1.2.0(transitive)
- Removedwhich@1.3.1(transitive)
- Removedwrappy@1.0.2(transitive)