While working on a web project (mainly PHP, JavaScript and CSS in this order) that I started few weeks ago I stumbled with re-factoring the source code such that it runs faster while becomes easier to maintain. OK, the code became shorter, it runs faster (fewer loops) but can it still be shorter (not necessarily running faster) ? The answer is yes.
Background
When the code is delivered from your web server to to client web browser it is delivered as-is. For instance if you deliver a JavaScript file (random-name.js) of 100KB you basically will have 100KB of upload/download between your web server-web browser. Multiply that by hundred of such pages then by ten thousands of daily visitors and what once was only 105 bytes become 1011 bytes. Btw, it is about 100GB of data traffic per day. If we can somehow cut this 100GB to half by doing ... nothing that will be a great achievement, isn't it?
Minify obfuscate JavaScript code
The technique it's quite simple: trim all white-spaces or other dispensable characters/symbols, remove the comments and other block delimiters, shorten the function/variable names and probably your code will lose some 2/3 of its weight. Of course, we don't want this "new code" on our development environment otherwise the code will become unmaintainable (see the first paragraph). The code will be shorted on its way from the development toward the production environment or even better, at runtime ,while the end-user will load the content from our web server. This process is called minification. This technique is old as the hills, Douglas Crockford did it more than a decade before. His C++ source code was made available and it's only 307 lines long. Looking at his code while writing this article I just realize that his code is just a parser specialized on removing the unnecessary white-spaces and/or the code comments. Actually this is the way that such a tool should be designed: build a code parser that understands the source language it deals with and that's it. I did it before by writing a parser for Assembly (an Assembly and a SQL editor plug-in for Eclipse) so I already knew that this was the right way of doing it. But I wanted to try achieving this only with regular expressions (aka regex) and such the YAYUI had borned.
What is YAYUI
YAYUI is just a tool that helps you to minify obfuscate the JavaScript code. You might already have heard of YUI Compressor and just like you I have realized that there are similar tools like mine so this one will be Yet Another YUI compressor, thus YAYUI.
You can test/workout the application at http://yayui.mynixworld.info. I just realized that this minifier (which has also code obfuscation capabilities) is around 300 lines of code, too. Moreover, comparing with other similar tools that are available online I saw that YAYUI produces more/less the same output so that's not bad!
Below I embed some info I made available at YAYUI web page:
Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.
Eugen Mihailescu
Latest posts by Eugen Mihailescu (see all)
- Dual monitor setup in Xfce - January 9, 2019
- Gentoo AMD Ryzen stabilizator - April 29, 2018
- Symfony Compile Error Failed opening required Proxies - January 22, 2018