Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2691 from willy-b/issue-2682
Browse files Browse the repository at this point in the history
Fix #2682: Make getBaseDomain use LRU cache
  • Loading branch information
bbondy authored Jul 25, 2016
2 parents f1714f6 + c0ccd26 commit a204271
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions js/lib/baseDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,5 +54,7 @@ module.exports.getBaseDomain = function (hostname) {
tld--
}

cachedBaseDomain.put(curDomain)

return curDomain
}

0 comments on commit a204271

Please sign in to comment.