I’ve been playing a bit with regular expressions lately and have to find some useful tasks in order to practice the skill. So, today I wanted to make a little method that strips HTML comments from an ASP.NET webpage at runtime. The practical use of the exercise is somewhat limited for most developers, but some websites have so many comments that it might just save a decent amount of bytes from the response stream.

The problem with this exercise is that a lot of JavaScript is using HTML comments to hide it’s workings from older browsers. That would mean that those script tags would be empty. That’s why I made a rule saying that every JavaScript has to implement the HTML comments correctly. Some don’t so you have to change it your self.

This is how the JavaScript is wrongly commented which also breaks my regex.

<script type="text/javascript">

<!--

  function Name()

  {  

  }

-->

</script>

The commenting should look like this which is also the right way to do it.

<script type="text/javascript">

<!--//

  function Name()

  {  

  }

//-->

</script>

The regular expression is very simple and all you need to do is to add the following method to your webpage, user control or master page.

using System.IO;

using System.Text;

using System.Text.RegularExpressions;

 

private static Regex _Regex = new Regex("((<!-- )((?!<!-- ).)*( -->))(\\r\\n)*", RegexOptions.Singleline);

 

protected override void Render(HtmlTextWriter writer)

{

  using (StringWriter sw = new StringWriter())

  {

    using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sw))

    {

      base.Render(htmlWriter);

      writer.Write(_Regex.Replace(sw.ToString(), string.Empty));

    }

  }
}

Maybe not the most useful stuff I've ever written, but fine for learning. The only thing that bugs me is the JavaScript rule.

>

After I wrote about a HTTP compression module in ASP.NET 2.0 one of my colleagues pointed out that the Deflate compression is faster than GZip. Because the HTTP compression module chooses GZip over Deflate if the browser allows it, I thought that I’d better make a quick performance test just to be sure. I used this little test method to give me the answer I was looking for:

using System.IO.Compression;

using System.IO;

using System.Diagnostics;

 

private void PerformanceTest()

{

  byte [] buffer = new byte [ 5000 ];

  using ( MemoryStream stream = new MemoryStream ())

  {

    Stopwatch sw = new Stopwatch ();

    sw.Start();

    for ( int i = 0 ; i < 1000 ; i++)

    {

      GZipStream gzip = new GZipStream (stream, CompressionMode .Compress);

      gzip.Write(buffer, 0 , buffer.Length);

    }

 

    sw.Stop();

    Response.Write(sw.ElapsedMilliseconds);

  }
}

First I tested the GZipStream and then the DeflateStream. I expected a minor difference because the two compression methods are different, but the result astonished me. I measured the DeflateStream to 41% faster than GZip. That’s a very big difference. With this knowledge, I’ll have to change the HTTP compression module to choose Deflate over GZip.