Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

regex-cache

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regex-cache - npm Package Compare versions

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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc