C Programming Tutorials and advice
CProgrammingTrends News Archives About Us Feedback


Click to Play

Social Media's Bad and Ugly Side
With so many positive aspects of social media, it's hard to think that there could be a downside. But unfortunately, there are current trends that reveal both a bad...

Recent Articles

How To Use C Programming To Verify Your Javascript
In the past few days, I've worked on finding a way to do static code analysis on JavaScript files.The resaon is that I want to apply some sort of binary and source code checking like FxCop and StyleCop provides for C#.

Pointers And Their Pitfalls
This may seem like a very basic idea, but I have seen far too many programmers not know the proper way to handle pointers. Pointers were the first thing I learned about in my second programming class...

Worldwide Programming Competition To Begin Soon
Keep an eye out - or get your application in - if you're interested in a competition that should unearth and crown some of the world's best programmers.

CodePlex Foundation, MySpace's Qizmt
MySpace announced that they're open sourcing Qizmt, a MapReduce framework used by the MySpace Data Mining team. Unlike other leading MapReduce frameworks...

August TIOBE Index Indicates Growth
The C, C++, and C# programming languages all performed well in the latest TIOBE Programming Community Index. One of them held its ground in terms of overall rankings, while the other two managed...

Garbage Collection And C
Garbage collection is an extremely undervalued aspect of modern society. After all, we couldn't be buzzing around in hybrids and enjoying our plasma TVs if we were...

"Concepts" Separated From C++0x
More information regarding the next C++ standard has become available, but unfortunately, the news isn't all that good. The release date remains a while away...



11.19.09

Using C Programming To Simplify JavaScripting With Closure Compiler


By Mads Kristensen

A few days ago, Google released their Closure Compiler project for optimizing JavaScript. Here's what they write about the Closure Compiler:

The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left.

The interesting part of the Closure Compiler is that it not only removes whitespace, it also rewrites your JavaScript code to make it smaller and optimizes the code for better performance. My tests show that it can reduce JavaScript files by about 60% - and that's before HTTP compression! Considering how much JavaScript a modern website uses, this is no less than amazing and highly useful.

The Closure Compiler comes in two flavors - a Java based command line tool and a RESTful API. I've been playing around with the API and it works great and very fast.
The code
The C# class I've written takes a JavaScript file and passes it through the API and then returns the compressed JavaScript as a string. The class contains one public and one private method and is only 47 lines of code including 16 lines of comments.


publicstring Compress(string file)

{

  string source = File.ReadAllText(file);

  XmlDocument xml = CallApi(source);

  return xml.SelectSingleNode("//compiledCode").InnerText;

}

 

privatestaticXmlDocument CallApi(string source)

{

  using (WebClient client = newWebClient())

  {

    client.Headers.Add("content-type", "application/x-www-form-urlencoded");

    string data = string.Format(PostData, HttpUtility.UrlEncode(source));

    string result = client.UploadString(ApiEndpoint, data);

 

    XmlDocument doc = newXmlDocument();

    doc.LoadXml(result);

    return doc;

  }

}

How to use it

You can use the class to do various cool things. You can write a MSBuild or NAnt script that automatically compresses your JavaScript files as part of a continuous integration process or, as I prefer, write a HTTP handler to do the same but at runtime. Remember to output cache the compressed result. Here's an example of using the class from ASP.NET:

GoogleClosure gc = newGoogleClosure();

string script = gc.Compress(Server.MapPath("~/script.js"));

Remember that the class doesn't do any exception handling, so you might want to stick that in yourself.

Comments

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/
About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice





CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com





-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2009 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article


CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact