PAC-P004 · dnsresolve-not-hoisted

dnsResolve called repeatedly for the same host

low · Performance

Each dnsResolve(host) call crosses into the engine's resolver, even when the answer is cached. Two or more calls for the same argument in one evaluation double the cost for nothing. Resolve once, reuse the variable.

Why it matters

JavaScript does not memoise function calls, and PAC engines do not deduplicate identical dnsResolve arguments within one evaluation; each call pays at least the boundary crossing into the resolver and, on a cache miss, a full lookup. isInNet(dnsResolve(host), ...) written three times for three ranges is three lookups. Hoisting (var ip = dnsResolve(host);) makes it one. Hoisting reduces the cost but keeps the dependency; see PAC-P001 for removing it.

How to fix

var ip = dnsResolve(host); then use ip in every isInNet call.

Examples

Bad

function FindProxyForURL(url, host) {
  if (isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") ||
      isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") ||
      isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0")) {
    return "DIRECT";
  }
  return "PROXY proxy.corp.example:8080";
}
Open bad example in checker

Good

function FindProxyForURL(url, host) {
  if (isPlainHostName(host) || dnsDomainIs(host, ".corp.example")) {
    return "DIRECT";
  }
  var ip = dnsResolve(host);
  if (ip && (isInNet(ip, "10.0.0.0", "255.0.0.0") ||
             isInNet(ip, "172.16.0.0", "255.240.0.0") ||
             isInNet(ip, "192.168.0.0", "255.255.0.0"))) {
    return "DIRECT";
  }
  return "PROXY proxy.corp.example:8080";
}
Open good example in checker

Related rules

References