Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing out the performance gain of compressing JavaScript code

I have used 5 JavaScript compressors to compress a JavaScript library (JSMin, YUI compressor, Packer, closure compiler and UglifyJS)

Now I know that closure compiler is the winner in reducing the filesize. However, I also want to test out the performance gains. What would be a good way to do this?

I made a simple test page that uses all the library's public methods. Is there a tool for testing out the page speed of this test page? Eg. running it X times on a browser and return the average loading speed.

Thanks for your answers!

like image 737
Chielus Avatar asked May 24 '11 08:05

Chielus


1 Answers

There's no need to be complex about it:

<html>
<head>
    <script>
    var time = new Date();
    </script>
    <script src="..."></script>
    ... more scripts ... 
</head>

<body>
<script>
    document.write("Time: " + String((new Date() - time)/1000) + " seconds");
</script>
</body>
</html>

Scripts in the <head> generally load serially, so this should be a reasonable method for measuring script execution time. If you have scripts executing form <body onload="...">, then do the time elapsed calculation at the end of that function instead of the end of the body.

This method would not measure execution time for "asynchronous" functions executed via setTimeout or setInterval, but those shouldn't count against load time.

An alternative and likely simpler option is to use the javascript profiler built-in to Chrome or Safari's web inspector.

like image 107
Seth Avatar answered Nov 10 '22 05:11

Seth