<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Danny Jensen</title>
    <description>The latest articles on DEV Community by Danny Jensen (@dannyjensen).</description>
    <link>https://dev.to/dannyjensen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4274650%2Fe5906391-ab9b-42d8-90f5-d28189b8ade0.png</url>
      <title>DEV Community: Danny Jensen</title>
      <link>https://dev.to/dannyjensen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dannyjensen"/>
    <language>en</language>
    <item>
      <title>How Nordtally filters 1,505 bot patterns in about 29 microseconds</title>
      <dc:creator>Danny Jensen</dc:creator>
      <pubDate>Mon, 30 Nov 2026 15:53:04 +0000</pubDate>
      <link>https://dev.to/dannyjensen/how-nordtally-filters-1505-bot-patterns-in-about-29-microseconds-43ma</link>
      <guid>https://dev.to/dannyjensen/how-nordtally-filters-1505-bot-patterns-in-about-29-microseconds-43ma</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That decision has to be cheap. It also has to be right enough that the dashboard is not full of crawlers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this sits on the hot path
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So the filter runs early, on every request. The drops are counted and shown in the compliance centre.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the filter actually checks
&lt;/h2&gt;

&lt;p&gt;There are four drop reasons. I keep the names stable because they are stored:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an empty user agent&lt;/li&gt;
&lt;li&gt;a prefetch (the Purpose, Sec-Purpose, X-Purpose or X-Moz header says prefetch, prerender or preview)&lt;/li&gt;
&lt;li&gt;a headless or obviously non-browser client, from a short list of markers such as HeadlessChrome, Lighthouse, Puppeteer, curl and wget&lt;/li&gt;
&lt;li&gt;a crawler, from a pattern list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The crawler list is &lt;a href="https://github.com/monperrus/crawler-user-agents" rel="noopener noreferrer"&gt;crawler-user-agents&lt;/a&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  1,469 plain strings, 36 real regexes
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// literals is keyed by the first two bytes of each plain pattern.&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lit&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;literals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;bigram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HasPrefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lit&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;short&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// patterns shorter than two bytes&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;regexes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 36 compiled patterns, run one by one&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MatchString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What that costs
&lt;/h2&gt;

&lt;p&gt;I timed this on the ingest code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;about 29 microseconds for a normal browser user agent&lt;/li&gt;
&lt;li&gt;about 1.7 microseconds for a known crawler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bots are not the same as AI referrals
&lt;/h2&gt;

&lt;p&gt;This distinction matters.&lt;/p&gt;

&lt;p&gt;GPTBot and other crawlers should be dropped. They are machines fetching pages.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this does not claim
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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. &lt;a href="https://nordtally.com" rel="noopener noreferrer"&gt;nordtally.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/monperrus/crawler-user-agents" rel="noopener noreferrer"&gt;crawler-user-agents&lt;/a&gt; (MIT), the pattern list Nordtally vendors&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nordtally.com/docs/counting" rel="noopener noreferrer"&gt;How Nordtally counts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nordtally.com/cookieless-analytics" rel="noopener noreferrer"&gt;Cookieless analytics, and what you give up&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nordtally.com/guides/analytics-consent" rel="noopener noreferrer"&gt;Consent for web analytics, country by country&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nordtally.com/pricing" rel="noopener noreferrer"&gt;Pricing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted on the &lt;a href="https://nordtally.com/blog/bot-filter" rel="noopener noreferrer"&gt;Nordtally blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I had help from an assistant on the first draft, then I edited it myself.&lt;/p&gt;

</description>
      <category>go</category>
      <category>analytics</category>
      <category>privacy</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
