From e9968713e46a77536aecb0599976bf2732a84fd9 Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 28 Nov 2017 13:19:57 -0500 Subject: [PATCH] work toward resolving #853 --- src/js/cookies.js | 2 +- src/js/matrix.js | 38 +++++++++++++++----------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/js/cookies.js b/src/js/cookies.js index 8ab3168..9579d12 100644 --- a/src/js/cookies.js +++ b/src/js/cookies.js @@ -475,7 +475,7 @@ var canRemoveCookie = function(cookieKey, srcHostnames) { srcHostname = cookieHostname; var pos; for (;;) { - if ( srcHostnames.hasOwnProperty(srcHostname) ) { + if ( srcHostnames.has(srcHostname) ) { if ( µm.mustAllow(srcHostname, cookieHostname, 'cookie') ) { return false; } diff --git a/src/js/matrix.js b/src/js/matrix.js index 155576d..e01d79d 100644 --- a/src/js/matrix.js +++ b/src/js/matrix.js @@ -554,29 +554,21 @@ Matrix.prototype.evaluateSwitchZ = function(switchName, srcHostname) { /******************************************************************************/ -// TODO: In all likelyhood, will have to optmize here, i.e. keeping an -// up-to-date collection of src hostnames with reference count etc. - -Matrix.prototype.extractAllSourceHostnames = function() { - var srcHostnames = {}; - for ( var rule of this.rules.keys() ) { - srcHostnames[rule.slice(0, rule.indexOf(' '))] = true; - } - return srcHostnames; -}; - -/******************************************************************************/ - -// TODO: In all likelyhood, will have to optmize here, i.e. keeping an -// up-to-date collection of src hostnames with reference count etc. - -Matrix.prototype.extractAllDestinationHostnames = function() { - var desHostnames = {}; - for ( var rule of this.rules.keys() ) { - desHostnames[this.desHostnameFromRule(rule)] = true; - } - return desHostnames; -}; +Matrix.prototype.extractAllSourceHostnames = (function() { + var cachedResult = new Set(); + var readTime = 0; + + return function() { + if ( readTime !== this.modifiedTime ) { + cachedResult.clear(); + for ( var rule of this.rules.keys() ) { + cachedResult.add(rule.slice(0, rule.indexOf(' '))); + } + readTime = this.modifiedTime; + } + return cachedResult; + }; +})(); /******************************************************************************/