<?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: RomanT</title>
    <description>The latest articles on DEV Community by RomanT (@romanti3172).</description>
    <link>https://dev.to/romanti3172</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%2F4250052%2F5b58c58d-1fcc-4bc3-a2fb-e120b2cd2f35.png</url>
      <title>DEV Community: RomanT</title>
      <link>https://dev.to/romanti3172</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/romanti3172"/>
    <language>en</language>
    <item>
      <title>Parsing HTML in .NET 10–30x faster than HtmlAgilityPack and AngleSharp: a list of tags instead of a DOM</title>
      <dc:creator>RomanT</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:46:26 +0000</pubDate>
      <link>https://dev.to/romanti3172/parsing-html-in-net-10-30x-faster-than-htmlagilitypack-and-anglesharp-a-list-of-tags-instead-of-a-2cab</link>
      <guid>https://dev.to/romanti3172/parsing-html-in-net-10-30x-faster-than-htmlagilitypack-and-anglesharp-a-list-of-tags-instead-of-a-2cab</guid>
      <description>&lt;p&gt;I have been scraping catalogs, prices, product cards and listings for years. All that time a small homegrown HTML parser did the work: millions of pages, no complaints. It never went public because its tests covered only my own sites and there were no docs.&lt;/p&gt;

&lt;p&gt;Last week I cleaned it up and released it as &lt;strong&gt;&lt;a href="https://www.nuget.org/packages/z3l.FastHtmlParser" rel="noopener noreferrer"&gt;z3l.FastHtmlParser&lt;/a&gt;&lt;/strong&gt;. The first version answered "how do I find a tag fast". Since then it has gone through five releases in one week, driven by feedback: XPath 1.0, CSS selectors, a review against hostile markup, and parsing straight from a &lt;code&gt;char[]&lt;/code&gt; buffer. This post covers the idea, the numbers and the trade-offs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package z3l.FastHtmlParser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A DOM is more than a scraper needs
&lt;/h2&gt;

&lt;p&gt;In a high-volume scraping service the parser is the hottest loop, and your server bill grows with it.&lt;/p&gt;

&lt;p&gt;HtmlAgilityPack and AngleSharp are excellent libraries, but they are built to be browsers. They create an object for every node, synthesize the implicit &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;tbody&amp;gt;&lt;/code&gt;, and keep a mutable tree so you can edit it and walk it with selectors.&lt;/p&gt;

&lt;p&gt;A scraper does far less: find an element, read its text or an attribute, move on. So the idea is simple: &lt;strong&gt;don't build what you won't use.&lt;/strong&gt; The document is the source string plus a flat list of tags, each one a set of integer offsets into that string.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;p&gt;BenchmarkDotNet 0.15.8, .NET 10, Intel Xeon E-2236, Windows Server 2025. Pages are real saved pages of books.toscrape.com, quotes.toscrape.com and webscraper.io test sites. AngleSharp 1.10.0, HtmlAgilityPack 1.13.1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parse a page, time&lt;/strong&gt; (lower is better):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Page&lt;/th&gt;
&lt;th&gt;FastHtmlParser&lt;/th&gt;
&lt;th&gt;HtmlAgilityPack&lt;/th&gt;
&lt;th&gt;AngleSharp&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;quotes, 11 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;17 μs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;155 μs&lt;/td&gt;
&lt;td&gt;229 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;laptops, 38 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;53 μs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;544 μs&lt;/td&gt;
&lt;td&gt;959 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books, 51 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;59 μs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;610 μs&lt;/td&gt;
&lt;td&gt;958 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books, no end tags*&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;59 μs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;671 μs&lt;/td&gt;
&lt;td&gt;949 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books ×10, 510 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.61 ms&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;19.6 ms&lt;/td&gt;
&lt;td&gt;12.2 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Parse a page, memory allocated:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Page&lt;/th&gt;
&lt;th&gt;FastHtmlParser&lt;/th&gt;
&lt;th&gt;HtmlAgilityPack&lt;/th&gt;
&lt;th&gt;AngleSharp&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;quotes, 11 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;11 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;243 KB&lt;/td&gt;
&lt;td&gt;189 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;laptops, 38 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;31 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;713 KB&lt;/td&gt;
&lt;td&gt;608 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books, 51 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;39 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;812 KB&lt;/td&gt;
&lt;td&gt;663 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books, no end tags*&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;39 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;756 KB&lt;/td&gt;
&lt;td&gt;644 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;books ×10, 510 KB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;391 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8.1 MB&lt;/td&gt;
&lt;td&gt;6.3 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;* The books page with all 75 &lt;code&gt;&amp;lt;/li&amp;gt;&lt;/code&gt; and 60 &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt; removed, as on sloppy sites.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parse and extract 20 books&lt;/strong&gt; (title, price, rating). All libraries return exactly the same records; the setup checks it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Library&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Allocated&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;z3l.FastHtmlParser&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;69 μs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;45 KB&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;… with CSS selectors&lt;/td&gt;
&lt;td&gt;75 μs&lt;/td&gt;
&lt;td&gt;45 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;… with XPath&lt;/td&gt;
&lt;td&gt;109 μs&lt;/td&gt;
&lt;td&gt;173 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HtmlAgilityPack (XPath)&lt;/td&gt;
&lt;td&gt;760 μs&lt;/td&gt;
&lt;td&gt;1,038 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AngleSharp (CSS selectors)&lt;/td&gt;
&lt;td&gt;1,043 μs&lt;/td&gt;
&lt;td&gt;707 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The interesting row is the CSS one: &lt;strong&gt;the same selectors AngleSharp runs are about 14x faster here, with 16x less memory.&lt;/strong&gt; You don't have to learn a new API to get most of the speed.&lt;/p&gt;

&lt;p&gt;On a real 1 MB Amazon search results page (100+ inline scripts, ~4,000 tags) parsing takes 0.65 ms against 11.3 ms (HtmlAgilityPack) and 15.1 ms (AngleSharp), and extracting 27 products takes 0.87 ms against 16.3 ms and 18.1 ms, with 23–29x less memory. That page isn't in the repo for copyright reasons; save your own as &lt;code&gt;benchmarks/local/amazon*.html&lt;/code&gt; and run &lt;code&gt;LocalScrapeBenchmarks&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the API looks like
&lt;/h2&gt;

&lt;p&gt;Three ways to query, pick whichever you like. All of them work on the document or inside a tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;z3l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FastHtmlParser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HtmlParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// 1. Find / FindAll: enums for tag and attribute names, no allocations while matching&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FindAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TagQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"product_pod"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;H3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AttributeByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlAttribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"price_color"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Text&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="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AttributeHref&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="c1"&gt;// absolute URL, resolved against the page&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 2. CSS selectors, as querySelectorAll / querySelector in a browser&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"article.product_pod h3 &amp;gt; a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div.quote:has(a.tag[href*=love]) span.text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CssFirst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ul#menu &amp;gt; li:last-child a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AttributeHref&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="c1"&gt;// 3. XPath 1.0&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;XPathValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"//a[contains(@href, '/product/')]/@href"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;XPathValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"//dt[.='Price']/following-sibling::dd[1]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// compile once, reuse on any number of pages and threads&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;CssQuery&lt;/span&gt; &lt;span class="nc"&gt;Prices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CssQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div.card span.price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Prices&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;p&gt;Chains are null-safe: every method accepts &lt;code&gt;null&lt;/code&gt; and returns &lt;code&gt;null&lt;/code&gt;, so a missing element never throws in the middle of a chain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Div&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"contacts"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
               &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HtmlTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"phone"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
               &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AttributeHrefTelephone&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="c1"&gt;// null if any step is missing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. A tag list instead of a tree
&lt;/h3&gt;

&lt;p&gt;Each tag stores only integers: where it starts and ends, where its inner content starts and ends, its depth and its parent. Text, inner HTML and attributes are computed on demand as slices of the source. Descendants of a tag are simply the tags that follow it in the list until one starts past its end, so "search inside a tag" is a plain indexed &lt;code&gt;for&lt;/code&gt; loop over a range.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Memory allocated once, at the exact size
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;'&amp;lt;'&lt;/code&gt; can't appear fewer times than there are tags, so a vectorized &lt;code&gt;Count('&amp;lt;')&lt;/code&gt; gives an upper bound. Working arrays are rented from &lt;code&gt;ArrayPool&amp;lt;T&amp;gt;.Shared&lt;/code&gt;, the final list is created once with &lt;code&gt;CollectionsMarshal.SetCount&lt;/code&gt;, and the rented arrays are cleared before they go back to the pool, so they don't keep the page alive. No &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; doubling, no copies.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Vectorized scanning
&lt;/h3&gt;

&lt;p&gt;Outside a tag only &lt;code&gt;'&amp;lt;'&lt;/code&gt; matters, found with vectorized &lt;code&gt;IndexOf&lt;/code&gt;. Inside a tag only &lt;code&gt;'&amp;lt;'&lt;/code&gt;, &lt;code&gt;'='&lt;/code&gt; and &lt;code&gt;'&amp;gt;'&lt;/code&gt; matter, found with &lt;code&gt;IndexOfAny&lt;/code&gt;. Quoted attribute values are skipped as a block, so &lt;code&gt;data-x="&amp;lt;p&amp;gt;...&amp;lt;/p&amp;gt;"&lt;/code&gt; doesn't break anything. &lt;code&gt;script&lt;/code&gt;, &lt;code&gt;style&lt;/code&gt;, &lt;code&gt;textarea&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt; and the other raw-text elements are scanned straight to their end tag, as HTML5 says.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Tag names without strings
&lt;/h3&gt;

&lt;p&gt;Names are &lt;code&gt;ReadOnlySpan&amp;lt;char&amp;gt;&lt;/code&gt; looked up through &lt;code&gt;Dictionary&amp;lt;string, HtmlTag&amp;gt;.AlternateLookup&amp;lt;ReadOnlySpan&amp;lt;char&amp;gt;&amp;gt;&lt;/code&gt; (new in .NET 9) and turned into an enum. I tried &lt;code&gt;FrozenDictionary&lt;/code&gt; and dropped it: about 16 ms of JIT warm-up on first use against ~2 ms for a plain &lt;code&gt;Dictionary&lt;/code&gt;, and slower lookups on this workload. A custom comparer hashes the length plus three case-folded characters. The same trick drives the table of all 2,125 HTML5 named entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Queries without IEnumerable
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Find&lt;/code&gt;, &lt;code&gt;Parent&lt;/code&gt;, &lt;code&gt;Next&lt;/code&gt; and &lt;code&gt;NextSibling&lt;/code&gt; return a single tag through an indexed loop; &lt;code&gt;TagQuery&lt;/code&gt; is a struct passed by &lt;code&gt;in&lt;/code&gt;. A chain like &lt;code&gt;doc.Find(...).Find(...).NextSibling(...)&lt;/code&gt; allocates nothing on the heap; the only allocation is the final &lt;code&gt;Text()&lt;/code&gt; string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Broken HTML
&lt;/h2&gt;

&lt;p&gt;Clean test pages are easy. Real pages leave out &lt;code&gt;&amp;lt;/li&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;/td&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/option&amp;gt;&lt;/code&gt;, and a naive parser then nests every &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; inside the previous one.&lt;/p&gt;

&lt;p&gt;I tried three fixes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Full HTML5 implicit-close rules in the main loop.&lt;/strong&gt; Correct, but 4–9% slower.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast pass, then a local re-parse on trouble.&lt;/strong&gt; Fell apart on the books page without end tags: all 20 books sit in one &lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt;, so most of the page was rescanned. 61 μs became 119 μs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the top of the open-tag stack when a tag opens.&lt;/strong&gt; If a new &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; opens while an unclosed &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; is on top, they're siblings: close the previous one. One integer comparison. This is the one that shipped.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result is in the table above: the books page with all 75 &lt;code&gt;&amp;lt;/li&amp;gt;&lt;/code&gt; and 60 &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt; removed parses in the same 59 μs as the original.&lt;/p&gt;

&lt;p&gt;Then came a review against hostile and random markup (version 1.3.0), and it found real bugs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;a title="see id=5" id="real"&amp;gt;&lt;/code&gt; reported the id as &lt;code&gt;5&lt;/code&gt;: attribute lookup now never looks inside another attribute's quoted value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;br/&amp;gt;&lt;/code&gt; without a space was named &lt;code&gt;br/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thousands of unclosed tags followed by stray end tags took &lt;strong&gt;seconds&lt;/strong&gt;. Parsing is now linear on any markup.&lt;/li&gt;
&lt;li&gt;In 1.4.1, XPath like &lt;code&gt;//div//span//a&lt;/code&gt; on deeply nested or unclosed markup was quadratic: 20,000 nested tags took 16 s and gigabytes of memory. Now 3 ms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test suite runs every saved page and a set of broken-markup snippets through AngleSharp too, and checks that both find the same elements, texts, attribute values and parents, CSS selector results included. The only differences are the trees a browser builds implicitly, like an inserted &lt;code&gt;&amp;lt;tbody&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parsing from a buffer
&lt;/h2&gt;

&lt;p&gt;If your HTTP client already reads into a pooled &lt;code&gt;char[]&lt;/code&gt;, you don't have to make a string out of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MemoryPool&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Shared&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxChars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Span&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HtmlParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// Dispose returns the buffer to the pool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to use something else
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You need to modify the document.&lt;/strong&gt; It's read-only (which is also why it's safe to query from many threads).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You need the browser's implicit tree.&lt;/strong&gt; No synthesized &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;tbody&amp;gt;&lt;/code&gt;; &lt;code&gt;table &amp;gt; tr&lt;/code&gt; matches as written in the source.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You're on .NET 8 or older.&lt;/strong&gt; The design depends on &lt;code&gt;AlternateLookup&lt;/code&gt;, so it targets .NET 9 and .NET 10 only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You need JavaScript-rendered content.&lt;/strong&gt; It's a parser, not a browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A week ago CSS selectors and XPath were on this list. They aren't anymore.&lt;/p&gt;

&lt;h2&gt;
  
  
  License
&lt;/h2&gt;

&lt;p&gt;Free for individuals and for companies with under $1M annual revenue and fewer than 100 employees, commercial use included. Larger companies pay once: $1,000 per organization, perpetual, all versions, and it covers every z3l library. The source is on GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/romanti3172/z3l.FastHtmlParser" rel="noopener noreferrer"&gt;https://github.com/romanti3172/z3l.FastHtmlParser&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;NuGet: &lt;a href="https://www.nuget.org/packages/z3l.FastHtmlParser" rel="noopener noreferrer"&gt;https://www.nuget.org/packages/z3l.FastHtmlParser&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Docs: &lt;a href="https://romanti3172.github.io/z3l.FastHtmlParser/" rel="noopener noreferrer"&gt;https://romanti3172.github.io/z3l.FastHtmlParser/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Benchmarks and how to run them: &lt;a href="https://github.com/romanti3172/z3l.FastHtmlParser/blob/main/benchmarks/README.md" rel="noopener noreferrer"&gt;benchmarks/README.md&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'd like to hear where it breaks: send a page it parses differently from your browser, and I'll add it to the tests.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>webscraping</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
