From c1d3b6222ee853b42c000e7d0f6c12b90cf5fa63 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 5 Apr 2018 09:45:11 -0400 Subject: [PATCH] code review: use regex to speed up CSS selector validation --- src/js/static-ext-filtering.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/js/static-ext-filtering.js b/src/js/static-ext-filtering.js index c4da31bd7..59fc173ed 100644 --- a/src/js/static-ext-filtering.js +++ b/src/js/static-ext-filtering.js @@ -1,7 +1,7 @@ /******************************************************************************* uBlock Origin - a browser extension to block requests. - Copyright (C) 2017 Raymond Hill + Copyright (C) 2017-2018 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -94,7 +94,11 @@ } catch (ex) { matchesFn = div.querySelector.bind(div); } + // Quick regex-based validation -- most cosmetic filters are of the + // simple form and in such case a regex is much faster. + var reSimple = /^[#.][\w-]+$/; return function(s) { + if ( reSimple.test(s) ) { return true; } try { matchesFn(s + ', ' + s + ':not(#foo)'); } catch (ex) {