JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2688034
  • Score
    100M100P100Q201006F
  • License

Memoize the results of a call to the RegExp constructor. Whenever possible, avoiding repetitious runtime compilation of the same string and options can result in dramatic speed improvements.

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 NPM version Build Status

Memoize the results of a call to the RegExp constructor. Whenever possible, avoiding repetitious runtime compilation of the same string and options can result in dramatic speed improvements.

Install with npm

npm i regex-cache --save

Usage

If the lib returns a function, you can wrap it like this:

var cache = require('regex-cache');
var regex = cache(require('some-regex-lib'));

Or, if the lib (or your own function) returns a regex, you can wrap it with a function first to achieve the same results.

Recommendations

  • do not use this if you are passing options
  • do use this if you are passing options and the logic to create the regex is extensive
  • do use this if you're not passing options and the lib returns a function (e.g. re().test(str))

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 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 09, 2015.