diff --git a/js/lib/baseDomain.js b/js/lib/baseDomain.js index 189d4a25979..968f4e00898 100644 --- a/js/lib/baseDomain.js +++ b/js/lib/baseDomain.js @@ -5,16 +5,26 @@ const punycode = require('punycode') const publicSuffixes = require('./psl') +const LRUCache = require('lru_cache/core').LRUCache + +let cachedBaseDomain = new LRUCache(50) + /** * Returns base domain for specified host based on Public Suffix List. * @param {string} hostname The name of the host to get the base domain for */ + module.exports.getBaseDomain = function (hostname) { // decode punycode if exists if (hostname.indexOf('xn--') >= 0) { hostname = punycode.toUnicode(hostname) } + let baseDomain = cachedBaseDomain.get(hostname) + if (baseDomain) { + return baseDomain + } + // search through PSL var prevDomains = [] var curDomain = hostname @@ -44,5 +54,7 @@ module.exports.getBaseDomain = function (hostname) { tld-- } + cachedBaseDomain.put(curDomain) + return curDomain }