From 1e7e6f86a6b57ad5e962e7196108e9d6a676bc04 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 10 Sep 2019 13:59:28 -0400 Subject: [PATCH] Reuse existing Set/Map when calling scriptletFilteringEngine.retrieve Reuse permanent instances instead. The trailing `$` is used to denote these variables are register-like instances, i.e. their content is valid only for the duration of the call. (From now on I will use this convention throughout the code base.) --- src/js/scriptlet-filtering.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index 7477960e4..f2aebf6e3 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -342,6 +342,10 @@ } }; + const scriptlets$ = new Set(); + const exceptions$ = new Set(); + const scriptletToCodeMap$ = new Map(); + api.retrieve = function(request) { if ( scriptletDB.size === 0 ) { return; } if ( µb.hiddenSettings.ignoreScriptInjectFilters ) { return; } @@ -360,41 +364,41 @@ return; } - const scriptlets = new Set(); - const exceptions = new Set(); + scriptlets$.clear(); + exceptions$.clear(); scriptletDB.retrieve( hostname, - [ scriptlets, exceptions ] + [ scriptlets$, exceptions$ ] ); if ( request.entity !== '' ) { scriptletDB.retrieve( `${hostname.slice(0, -request.domain.length)}${request.entity}`, - [ scriptlets, exceptions ] + [ scriptlets$, exceptions$ ] ); } - if ( scriptlets.size === 0 ) { return; } + if ( scriptlets$.size === 0 ) { return; } const loggerEnabled = µb.logger.enabled; // Wholly disable scriptlet injection? - if ( exceptions.has('') ) { + if ( exceptions$.has('') ) { if ( loggerEnabled ) { logOne(true, '', request); } return; } - const scriptletToCodeMap = new Map(); - for ( const rawToken of scriptlets ) { - lookupScriptlet(rawToken, reng, scriptletToCodeMap); + scriptletToCodeMap$.clear(); + for ( const rawToken of scriptlets$ ) { + lookupScriptlet(rawToken, reng, scriptletToCodeMap$); } - if ( scriptletToCodeMap.size === 0 ) { return; } + if ( scriptletToCodeMap$.size === 0 ) { return; } // Return an array of scriptlets, and log results if needed. const out = []; - for ( const [ rawToken, code ] of scriptletToCodeMap ) { - const isException = exceptions.has(rawToken); + for ( const [ rawToken, code ] of scriptletToCodeMap$ ) { + const isException = exceptions$.has(rawToken); if ( isException === false ) { out.push(code); }