About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
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 2014

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 4.9 by 12 people

  • Currently 4.916667/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 (19) | 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!

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.

Balaji in

Monday, December 01, 2008 9:17 PM

Balaji

any new version/updates for Mirion compression module?

Sri sg

Tuesday, January 20, 2009 6:31 PM

Sri

Just want to inform you that I am using your CSSMinifier modified with a option to skip part of a file from minification by enclosing in /@ @/ tags. If you require the modified file and any one else, I will be more than happy to send to you. Please send me a mail.

I am developing a commercial & free product using your code. Just want to inform you, so that If I am violating any copywrites and patents you can inform me so that I can remove the code from my product.

Websitesi tr

Wednesday, March 18, 2009 7:42 AM

Websitesi

Been looking for some kind of runtime minifier for projects i have been working in ASP.NET MVC. Came acros this blog post and i like it. Thanks for sharing

ozman gb

Wednesday, May 13, 2009 1:02 AM

ozman

@Sri: Do you have that modified code for CSSMinifier to skip /@ @/

HIPS gb

Tuesday, August 04, 2009 5:10 PM

HIPS

Thanks for writing this. It was very helpful. Keep writing.

André br

Wednesday, August 19, 2009 7:51 AM

André

Hi!

I didn't understand how can I use this on a MasterPage with my <link type="text/css" href="MyCSS.css" />!Thanks!

Miron il

Wednesday, August 19, 2009 2:04 PM

Miron

@Andre,
You can use my compression library that will do the job for you:
http://www.codeplex.com/MbCompression

Kerido us

Saturday, January 30, 2010 6:58 AM

Kerido

Take a look at my ultra fast CSS minifier: www.ko-sw.com/.../...ast-CSS-Minify-Algorithm.aspx