Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I reroute certain websites through a proxy with a proxy.pac file.

It basically looks like this:

if (shExpMatch(host, ""))
    { return "PROXY proxy.domain.tld:8080; DIRECT" }
if (shExpMatch(host, ""))
    { return "PROXY proxy.domain.tld:8080; DIRECT" }

At the moment about 125 sites are rerouted using this method. However, I plan on adding quite a few more domains to it, and I'm guessing it will eventually be a list of 500-1000 domains.

It's important to not reroute all traffic through the proxy.

What's the best way to keep this file optimized, performance-wise ?


share|improve this question
up vote 0 down vote accepted

As usual: hashes or trees.

I'd use hashing: extract the first one (or more chars) of the requested domain name (stripping "www." as well) to select corresponding pattern list.

share|improve this answer
Could you give me an example? – Tuinslak Mar 10 '11 at 11:32
Most straight one: host[0] would give you the very first char of it. Now you can use it as switch key: switch (host[ 0 ]) { case 'g': if ... else if () and so on (if's are from you example). – poige Mar 10 '11 at 13:05

If you are just checking for equality use '==' for comparison. The shExpMatch function allows for shell expressions (* and ? in their DOS shell meanings) so the second argument has to be parsed. The script runs in the browser, once (or less) per request so performance is not an issue but it makes the code clearer if you write what yuo mean.

I would also use a variable to hold the proxy expression. It probably won't save run-time storage as the repeated literal is probably re-used, but it will make the code easier to read.

There is good info on including examples and lessons learned.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.