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.)
This commit is contained in:
Raymond Hill 2019-09-10 13:59:28 -04:00
parent a73dd0a9f2
commit 1e7e6f86a6
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 16 additions and 12 deletions

View File

@ -342,6 +342,10 @@
} }
}; };
const scriptlets$ = new Set();
const exceptions$ = new Set();
const scriptletToCodeMap$ = new Map();
api.retrieve = function(request) { api.retrieve = function(request) {
if ( scriptletDB.size === 0 ) { return; } if ( scriptletDB.size === 0 ) { return; }
if ( µb.hiddenSettings.ignoreScriptInjectFilters ) { return; } if ( µb.hiddenSettings.ignoreScriptInjectFilters ) { return; }
@ -360,41 +364,41 @@
return; return;
} }
const scriptlets = new Set(); scriptlets$.clear();
const exceptions = new Set(); exceptions$.clear();
scriptletDB.retrieve( scriptletDB.retrieve(
hostname, hostname,
[ scriptlets, exceptions ] [ scriptlets$, exceptions$ ]
); );
if ( request.entity !== '' ) { if ( request.entity !== '' ) {
scriptletDB.retrieve( scriptletDB.retrieve(
`${hostname.slice(0, -request.domain.length)}${request.entity}`, `${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; const loggerEnabled = µb.logger.enabled;
// Wholly disable scriptlet injection? // Wholly disable scriptlet injection?
if ( exceptions.has('') ) { if ( exceptions$.has('') ) {
if ( loggerEnabled ) { if ( loggerEnabled ) {
logOne(true, '', request); logOne(true, '', request);
} }
return; return;
} }
const scriptletToCodeMap = new Map(); scriptletToCodeMap$.clear();
for ( const rawToken of scriptlets ) { for ( const rawToken of scriptlets$ ) {
lookupScriptlet(rawToken, reng, scriptletToCodeMap); lookupScriptlet(rawToken, reng, scriptletToCodeMap$);
} }
if ( scriptletToCodeMap.size === 0 ) { return; } if ( scriptletToCodeMap$.size === 0 ) { return; }
// Return an array of scriptlets, and log results if needed. // Return an array of scriptlets, and log results if needed.
const out = []; const out = [];
for ( const [ rawToken, code ] of scriptletToCodeMap ) { for ( const [ rawToken, code ] of scriptletToCodeMap$ ) {
const isException = exceptions.has(rawToken); const isException = exceptions$.has(rawToken);
if ( isException === false ) { if ( isException === false ) {
out.push(code); out.push(code);
} }