Wednesday, June 17, 2015

Learning JavaScript the hard way: 1 - dont minify in beta

Being a seasoned vet with C# i find the world of javascript rather sensible, although there are a few pitfalls. One difference is the use of minified files. Since javascript is parsed, rather than compiled there is a great push to make the source as small as possible, thus allowing for faster loading sites.

One way of achieving this would be to write javascript the same way C++ devs write their code; with truncated and unreadable variable names, and without any comments except the ever so loved "\\change this".

The good news is that javascript devs are not C++ autists, and the prefered solution is a fair bit more sane.

The common practice is called minifying. And it consists of parsing the source files thru a program that changes out every var with a short-as-possible name, and removes all unneded whitespace and comments. This makes the code unreadable for sane humans, but saves space.

 Below is an example of code minified using http://jscompress.com/

This is a normal un-minified javascript function (388 bytes)
This is the same function minified (188 bytes)
But back to the problems: Being new to javascript i just downloaded the minified versions of all my libs (angularJS, jQuery etc.) and this lead to problems during debugging, as all the error messages were less than readable.

Switching out the minified files with the large uncompressed ones made things alot easier.

No comments:

Post a Comment