Skip to content

Commit

Permalink
feat: regex cache (#12738)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
rarkins and viceice committed Nov 18, 2021
1 parent e095d5a commit e7b841a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/util/regex.ts
Expand Up @@ -5,6 +5,8 @@ import { logger } from '../logger';

let RegEx: RegExpConstructor;

const cache = new Map<string, RegExp>();

try {
// eslint-disable-next-line
const RE2 = require('re2');
Expand All @@ -18,8 +20,16 @@ try {
}

export function regEx(pattern: string | RegExp, flags?: string): RegExp {
const key = `${pattern.toString()}:${flags}`;

if (cache.has(key)) {
return cache.get(key);
}

try {
return new RegEx(pattern, flags);
const instance = new RegEx(pattern, flags);
cache.set(key, instance);
return instance;
} catch (err) {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = pattern.toString();
Expand Down

0 comments on commit e7b841a

Please sign in to comment.