Skip to content
Snippets Groups Projects
Unverified Commit e7b841a1 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

feat: regex cache (#12738)

parent e095d5a9
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,8 @@ import { logger } from '../logger'; ...@@ -5,6 +5,8 @@ import { logger } from '../logger';
let RegEx: RegExpConstructor; let RegEx: RegExpConstructor;
const cache = new Map<string, RegExp>();
try { try {
// eslint-disable-next-line // eslint-disable-next-line
const RE2 = require('re2'); const RE2 = require('re2');
...@@ -18,8 +20,16 @@ try { ...@@ -18,8 +20,16 @@ try {
} }
export function regEx(pattern: string | RegExp, flags?: string): RegExp { export function regEx(pattern: string | RegExp, flags?: string): RegExp {
const key = `${pattern.toString()}:${flags}`;
if (cache.has(key)) {
return cache.get(key);
}
try { try {
return new RegEx(pattern, flags); const instance = new RegEx(pattern, flags);
cache.set(key, instance);
return instance;
} catch (err) { } catch (err) {
const error = new Error(CONFIG_VALIDATION); const error = new Error(CONFIG_VALIDATION);
error.validationSource = pattern.toString(); error.validationSource = pattern.toString();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment