Socket
Socket
Sign inDemoInstall

regex-cache

Package Overview
Dependencies
3
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

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 dramatic speed improvements.


Version published
Maintainers
1
Install size
1.94 MB
Created

Readme

Source

regex-cache NPM version Build Status

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 --save

Usage

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 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.

FAQs

Last updated on 17 Feb 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc