Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
regex-cache
Advanced tools
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.
The regex-cache npm package is designed to cache the results of regular expression operations to improve performance. It is particularly useful when the same regular expressions are used repeatedly, as it avoids the overhead of recompiling the regex each time.
Caching Regular Expressions
This feature allows you to cache a regular expression. The first time the regex is used, it is compiled and cached. Subsequent uses of the same regex will retrieve it from the cache, improving performance.
const regexCache = require('regex-cache');
const re = regexCache(/foo/);
console.log(re.test('foo')); // true
Using Cached Regular Expressions
This feature demonstrates that the same regular expression is retrieved from the cache, ensuring that the same instance is used, which can save memory and processing time.
const regexCache = require('regex-cache');
const re1 = regexCache(/foo/);
const re2 = regexCache(/foo/);
console.log(re1 === re2); // true
The lru-cache package provides a least-recently-used (LRU) cache, which can be used to store any kind of data, including regular expressions. Unlike regex-cache, lru-cache is more general-purpose and can be used for caching various types of data.
The memoizee package is a general-purpose memoization library that can cache the results of function calls. It can be used to cache the results of functions that create regular expressions, providing similar performance benefits to regex-cache but with more flexibility.
The fast-memoize package is another memoization library focused on speed. It can be used to memoize functions that generate regular expressions, offering a high-performance alternative to regex-cache for caching regex results.
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.
npm i regex-cache --save
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
re().test(str)
)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)
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.
Install dev dependencies:
npm i -d && npm test
Install dev dependencies:
npm i -d && npm run benchmarks
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb on February 09, 2015.
FAQs
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.
The npm package regex-cache receives a total of 1,604,702 weekly downloads. As such, regex-cache popularity was classified as popular.
We found that regex-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.