| Index: lib/domain.js |
| =================================================================== |
| --- a/lib/domain.js |
| +++ b/lib/domain.js |
| @@ -15,16 +15,28 @@ |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| "use strict"; |
| const publicSuffixes = require("../data/publicSuffixList.json"); |
| /** |
| + * Maximum number of entries to keep in <code>{@link cache}</code>. |
| + * @type {number} |
| + */ |
| +const maxCacheEntries = 1000; |
| + |
| +/** |
| + * Cached results from previous <code>{@link isThirdParty}</code> calls. |
| + * @type {Map.<string,boolean>} |
| + */ |
| +let cache = new Map(); |
| + |
| +/** |
| * Checks whether the given hostname is a domain. |
| * |
| * @param {string} hostname |
| * @returns {boolean} |
| */ |
| function isDomain(hostname) |
| { |
| // No hostname or IPv4 address, also considering hexadecimal octets. |
| @@ -69,21 +81,37 @@ |
| * |
| * @param {URL} url The request URL. |
| * @param {string} documentHostname The IDNA-encoded hostname of the document. |
| * |
| * @returns {boolean} |
| */ |
| function isThirdParty(url, documentHostname) |
| { |
| - let requestHostname = url.hostname.replace(/\.+$/, ""); |
| - documentHostname = documentHostname.replace(/\.+$/, ""); |
| + let requestHostname = url.hostname; |
| + |
| + if (requestHostname[requestHostname.length - 1] == ".") |
|
Manish Jethani
2019/01/23 08:51:06
Avoid calling replace() since it's very rarely nec
|
| + requestHostname = requestHostname.replace(/\.+$/, ""); |
| + |
| + if (documentHostname[documentHostname.length - 1] == ".") |
| + documentHostname = documentHostname.replace(/\.+$/, ""); |
| if (requestHostname == documentHostname) |
|
Manish Jethani
2019/01/23 08:51:06
No need to cache if the hostnames are literally eq
|
| return false; |
| - if (!isDomain(requestHostname) || !isDomain(documentHostname)) |
| - return true; |
| + let key = requestHostname + " " + documentHostname; |
| + let value = cache.get(key); |
| + |
| + if (typeof value != "undefined") |
| + return value; |
| - return getDomain(requestHostname) != getDomain(documentHostname); |
| + value = !isDomain(requestHostname) || !isDomain(documentHostname) || |
| + getDomain(requestHostname) != getDomain(documentHostname); |
| + |
| + if (cache.size >= maxCacheEntries) |
| + cache.clear(); |
| + |
| + cache.set(key, value); |
| + |
| + return value; |
| } |
| exports.isThirdParty = isThirdParty; |