From e7b841a138667a72b368d03d827a41c88174854d Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 18 Nov 2021 15:36:05 +0100
Subject: [PATCH] feat: regex cache (#12738)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/util/regex.ts | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lib/util/regex.ts b/lib/util/regex.ts
index ac956bf18c..fe3479c444 100644
--- a/lib/util/regex.ts
+++ b/lib/util/regex.ts
@@ -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');
@@ -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();
-- 
GitLab