Efficient CSS Compression in Perl

At Booking.com we use Yahoo’s YUICompressor to minify our style sheet and JavaScript files. We do this automatically from Perl for a smooth development experience.

Simon Bertrang
Dec 14, 2012 · 2 min read
Image for post
Image for post

But since YUICompressor is written in Java it is quite expensive to start. Done in the staging phase, this is bearable even for the the very agile deployment strategy at Booking.com. Nonetheless, we found that this step slows down the development process. When we started experimenting with dynamic compression, it was easier to port the style sheet compressor logic to Perl than addressing the issues in Java. This resulted in the CSS::Compressor module on CPAN.

We were quite pleased when we found that the Perl port of the logic was faster than the original Java implementation. It turns out that the way that string handling was done in Java puts a lot more pressure on memory allocation than what happens when executing the Perl code that replaced it. It won’t be a surprise for anyone that Perl is good at string manipulation.

The average speedup initially measured was over 50% based on running the minification on a set of production files from several days. Due to the start-up overhead of the JVM, Perl’s CSS::Compressor wins more significantly on a shorter running process. For a larger sample, the Perl implementation still saved about 40% of run time compared to the Java code.

Our usual approach to replacing a component such as this is to run old and new side by side, validating the output without using the new code in anger just yet. It turns out that the only difference in results (apart from run times) were problems with the Java version that never manifested in the reimplementation.

During a recent Booking.com hackathon, we abstracted the code into a CPAN-style distribution, applied a bit of spit and polish and voila, released it to the public. Enjoy!

Would you like to be a Developer at Booking.com? Work with us!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store