About the author

Miron Abramson
Me
Software Engineer, Senior Developer at CapitalIQ, and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

High performance css minifier

It is known that minifying the JavaScript and CSS files can really reduce the files size and improve the general site performance. Lot of sites (BlogEngine.NET among them)  do it on run-time and not it the build time. At this point, I want to recommend the JavaScript minifier JSMIN by Douglas Crockford. It does the job very good, and by far, much faster than all the RegularExpression \ Replace minifiers. Because in my Compression project MbCompression I do the minifying on run-time, I decided to use jsmin minifier.

The Css minifier

All the Css minifiers I found are using Regular Expression\ Replace to remove the unneeded characters. This is working fine, but have a realy bad performance, special on run-time. The speed is slow (special using the Regular Expression), and another important thing is, every Replace creates a very big string in the memory! (Strings are immutable, remember?), so if you have several Replaces, it creates several big strings in the server memory!

To improve performance, I took  JSMIN idea, that are not using any strings in the memory, and perform the minifying much faster, and created a CSS minifier that produce a small CSS file, much faster, and with much less memory overhead.

This minifier was tested on several CSS files. Feel free to download it and use it. If there is any file that not been minify correctly, send it to me, and I will try to improve the minifier.

CssMinifier.cs (9.59 kb)

Currently rated 5.0 by 8 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C# | Client side | Performance
Posted by Miron on Sunday, June 29, 2008 4:48 AM
Permalink | Comments (12) | Post RSSRSS comment feed

Related posts

Comments

Nick Berardi us

Sunday, June 29, 2008 3:28 PM

Nick Berardi

Why not use the YUI Compressor, it is by far much better than JSMin and it does CSS and JavaScript. I even created a MSBuild task for it.

www.coderjournal.com/.../

Check it out.

Nick

Guan Tao's Blog cn

Sunday, June 29, 2008 8:04 PM

Guan Tao's Blog

Very good. I was learning ASP.NET, but do not know why, I do not understand the preparation of the code you!

Miron il

Sunday, June 29, 2008 10:56 PM

Miron

@Nick,
You right that YUI Compression produce a little smaller size minified files, however, as I wrote in the post, this minifier is for sites that make the minify in run-time and not in the Build time. For this kind of task, jsmin & my minifier are much better because they are much faster and much less memory overhead (and they are in C# ;-) )

Jason Huck us

Monday, June 30, 2008 10:38 PM

Jason Huck

Yes, YUI Compressor is slower than JSMin, but the results (safer, smaller) are worth it, IMO. We use a "lazy" caching technique at run-time in which we create a checksum based on the paths of the included files along with their modification dates, and use that as the filename for the resulting file, which is written to disk. After the initial creation of the compressed files, YUI Compressor is only invoked if you change your scripts or styles (which alters the checksum).

Miron il

Wednesday, July 02, 2008 1:31 PM

Miron

@Jason,
That's cool. My next release of my Compression library ( http://www.codeplex.com/MbCompression ) will let you choose if to use the technic of file system cache or use Output cache.
YUI Compression is not .NET code... do you invoke it as a process ?

season cn

Monday, July 07, 2008 5:55 PM

season

the blog is very cool! I like it!

关涛 cn

Thursday, July 10, 2008 4:33 AM

关涛

今天闲着无聊,我来光顾你博客了!哈哈!博客非常不错哦!越来越好了!我们一起加油!

Petr Snobelt cz

Thursday, July 10, 2008 7:30 PM

Petr Snobelt

I think you already know it, but when you working with strings, you should use stringBuilder.

Miron il

Thursday, July 10, 2008 11:18 PM

Miron

@Pert,
What are you talking about?
There is even no one string in the whole class!

Guan Tao Blog's cn

Monday, July 14, 2008 5:40 AM

Guan Tao Blog's

@Pert,
Yes!

Daniel gb

Saturday, August 30, 2008 4:04 AM

Daniel

You can get a C# port of YUI CssMin from my blog post here: www.dimebrain.com/2008/03/a-better-css-mi.html
There is also a port of the JavascriptCompressor in YUI as part of a complete port with MSBUILD support here: http://www.codeplex.com/YUICompressor

Miron il

Saturday, August 30, 2008 9:41 AM

Miron

@Daniel,
I prefer JSMIN and my css minifier over YUI minifiers. Mybe the final results of the YUI is few kb less, but jsmin & mine css minifier are much faster, and the memory print is much smaller.
I guess no many deveopers will use MSBuild for small to medium applications, so the minifing will be on run-time (and of-course will be cached), so speed & memory are more important than saving more few kb.

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

Sunday, September 07, 2008 4:03 AM