How Nordtally filters 1,505 bot patterns in about 29 microseconds
Most of the list is plain strings in a first-two-bytes index. The 36 real regexes run one by one.
Danny
I build Nordtally, a cookieless web analytics tool hosted in the EU. The script is under 1.5 KB, and a site can run in strict, anonymous or consented mode. This post is not a tour of the product. It is about one piece of the ingest path: deciding whether a request is a person or a bot, before anything is stored.
That decision has to be cheap. It also has to be right enough that the dashboard is not full of crawlers.
Why this sits on the hot path
Every event still arrives with an IP address and a User-Agent. I use both in memory for the length of one request: to look up the country, to run the bot check, and, in anonymous mode, to build a daily visitor hash. Neither the IP nor the full user agent is written down.
If I counted crawlers as visits, the numbers would be useless. Search bots, uptime monitors, AI crawlers and curl would all show up as traffic. Prefetch and prerender requests would inflate the same counters, even when nobody looked at the page. Requests from bots, prefetches and visitors who sent Global Privacy Control are filtered before storage and never count as a pageview.
Prefetch is easy to overlook. A browser can fetch a page the person has not opened yet, or generate a link preview. If I stored that, it would look like a visit. The same goes for an empty user agent: I do not try to guess what it was. I drop it.
So the filter runs early, on every request. The drops are counted and shown in the compliance centre.
What the filter actually checks
There are four drop reasons. I keep the names stable because they are stored:
- an empty user agent
- a prefetch (the Purpose, Sec-Purpose, X-Purpose or X-Moz header says prefetch, prerender or preview)
- a headless or obviously non-browser client, from a short list of markers such as HeadlessChrome, Lighthouse, Puppeteer, curl and wget
- a crawler, from a pattern list
The crawler list is crawler-user-agents by Martin Monperrus, MIT licensed, and I vendor it as-is. I did not curate 1,505 patterns by hand. I did have to decide how to run them on every request without adding milliseconds.
1,469 plain strings, 36 real regexes
The list is 1,505 patterns. Almost all of them are plain strings once you strip simple punctuation escapes. A few are actual regular expressions.
I put the 1,469 plain strings in an index keyed by their first two bytes. When a user agent comes in, I slide a two-byte window along it, look up that pair, and test the short candidate list with a prefix check. Patterns shorter than two characters go in a tiny side list and are matched with a contains check.
The remaining 36 patterns are real regexes. They are compiled and run one by one. That is deliberate. Go's regexp package is RE2, and a single pattern can use a literal-prefix fast path. Joining those 36 into one alternation would throw that away. There is regex in this filter. There are 36 of them. They just are not the common case.
A known crawler often matches early, on the indexed literals. An ordinary browser user agent has to be checked all the way through, including those 36 regexes, before I let it in.
Take a normal Chrome user agent. None of the plain crawler strings appear in it, so the index lookups do not help, the short list does not match, and all 36 regexes have to run and miss. That is the slow path, and it is still about 29 microseconds. Googlebot is the other way around: the two-byte window hits the start of that literal, the prefix matches, and I can stop. That is why a known crawler is about 1.7 microseconds.
A first-two-bytes index is not the most sophisticated string matcher I could have used. It is small, easy to reason about, and it was fast enough in the timings below. In outline the scan looks like this:
// literals is keyed by the first two bytes of each plain pattern.
for i := 0; i+1 < len(ua); i++ {
for _, lit := range literals[bigram(ua, i)] {
if strings.HasPrefix(ua[i:], lit) {
return true
}
}
}
for _, lit := range short { // patterns shorter than two bytes
if strings.Contains(ua, lit) {
return true
}
}
for _, re := range regexes { // 36 compiled patterns, run one by one
if re.MatchString(ua) {
return true
}
}What that costs
I timed this on the ingest code:
- about 29 microseconds for a normal browser user agent
- about 1.7 microseconds for a known crawler
I also timed a single combined regex of all 1,505 patterns. That came out at about 1.5 milliseconds. For an ordinary browser user agent, that is on the order of fifty times slower than the indexed literals plus the 36 separate regexes.
Those are microbenchmarks, not production histograms. I am not going to dress them up as a promise about every machine and every traffic mix. They are why I do not run the whole list as one combined regex.
Bots are not the same as AI referrals
This distinction matters.
GPTBot and other crawlers should be dropped. They are machines fetching pages.
A person who follows a link out of ChatGPT or Perplexity is not a crawler. That visit should count, and I want the dashboard to show that it came from there. Nordtally groups those sources so you can see AI referrals separately from search and social. The bot filter and the source report are doing different jobs.
What this does not claim
The filter will not catch every fake client, and a conservative marker list can drop a tool that a real person is using. I would rather miss a few odd clients than let Googlebot, GPTBot and Lighthouse inflate a site's numbers.
It also does not make a legal claim. Cookieless is not the same as "no consent needed." That depends on the country. Strict mode keeps no identifier at all. Anonymous mode uses a hash that is forgotten at midnight. Consented mode sets a first-party cookie only after the visitor agrees. I have written about that more carefully in the site guides, and it is not the subject of this post.
If you want to try the product: it is hosted in the EU, the script is under 1.5 KB, and the free plan goes up to 50,000 pageviews a month with no card. nordtally.com
Resources
- crawler-user-agents (MIT), the pattern list Nordtally vendors
- How Nordtally counts
- Cookieless analytics, and what you give up
- Consent for web analytics, country by country
- Pricing