# dnsResolve called repeatedly for the same host

Source: https://findproxyforurl.net/rules/dnsresolve-not-hoisted/ · last updated 2026-10-14

Rule PAC-P004 (`dnsresolve-not-hoisted`), severity low, category: Performance.

Repeated dnsResolve(host) calls for one name are usually answered from a cache, but Windows (WinHTTP) waits again on every call when DNS does not answer, pactester asks for every call, and in Chrome dnsResolve plus dnsResolveEx for one name are two lookups. Resolve once into a local variable.

## Why it matters

JavaScript does not remember the result of a function call, but the engines' DNS caches do.
In Chrome, Edge, Firefox and Windows a repeated `dnsResolve(host)` for a name that resolves is
answered from a cache: in the lab on 2026-10-14, 150 calls for one name in one run sent one
DNS query in each of them. Three `isInNet(dnsResolve(host), ...)` calls for three ranges are therefore not three
queries there.

Resolving once is still the right way to write it, for three reasons:

- **Windows (WinHTTP) keeps no table of names during one run.** When the DNS server does not
  answer, for example on a laptop outside the company network, every call waits for the timeout
  again, about 12 seconds on Windows 11: 10 calls made one request wait 120 seconds.
- **pactester (pacparser) and other engines without a cache** send a query to the DNS server
  for every call unless the system they run on caches answers.
- **Chrome and Edge store `dnsResolve` and `dnsResolveEx` for the same name as separate
  entries**, so a script that mixes them asks twice, and both count towards the limit of 20
  different names per request.

Keep the variable local to `FindProxyForURL`; a cache in a top-level variable is a different
problem (PAC-K008). Resolving once reduces the cost but keeps the dependency on DNS; see
PAC-P001 for removing it.


## How to fix

var ip = dnsResolve(host); inside FindProxyForURL, then use ip in every isInNet call and test a failed lookup with if (!ip), because Windows returns false instead of null.

## Bad

```js
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";
}
```

## Good

```js
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";
}
```

## Engine behaviour

- chromium: Keeps the answers of one request in a table, failures included, so a repeat of dnsResolve(host) costs no lookup. dnsResolve and dnsResolveEx for the same name are separate entries. Limit of 20 different names per request. (source: code, verified 2026-10-14)
- firefox: No table in the PAC engine; every call asks the browser's DNS cache, which answers repeats and keeps a failure for 60 s. 150 calls for one name sent one query in the lab. (source: lab, verified 2026-10-14)
- pacparser: No cache: every call asks the system's resolver again. 150 calls for one name sent 150 queries on a system without a caching DNS service. (source: lab, verified 2026-10-14)
- winhttp: Repeats for a name that resolves are answered from the Windows DNS cache (150 calls, one query). When the DNS server does not answer, every call waits about 12 s again, also inside one run (Windows 11, lab). (source: lab, verified 2026-10-14)
