diff --git a/src/CrawlerDetect.php b/src/CrawlerDetect.php index 5adc62c..4121fad 100644 --- a/src/CrawlerDetect.php +++ b/src/CrawlerDetect.php @@ -73,6 +73,15 @@ class CrawlerDetect */ protected $compiledExclusions; + /** + * Cache of compiled regex strings keyed by pattern-list hash, shared + * across instances so per-request `new CrawlerDetect` calls don't + * re-implode the (~1500-entry) pattern list each time. + * + * @var array + */ + protected static $compileCache = []; + /** * Class constructor. */ @@ -92,12 +101,21 @@ public function __construct(?array $headers = null, $userAgent = null) /** * Compile the regex patterns into one regex string. * + * A non-capturing group is used because callers only need the full + * match (preg_match's $matches[0]), not a back-reference. + * * @param array * @return string */ public function compileRegex($patterns) { - return '('.implode('|', $patterns).')'; + $key = md5(serialize($patterns)); + + if (! isset(self::$compileCache[$key])) { + self::$compileCache[$key] = '(?:'.implode('|', $patterns).')'; + } + + return self::$compileCache[$key]; } /**