JQuery has two versions for download, one is Production (19KB, Minified and Gzipped), and the other is Development (120KB, Uncompressed Code).
Now the compact 19kb version, if you download it, you will see is still a javascript executable code. How did they compactify it? And how can I 'minify' my code like that too?
DIY Minification
No minifier can compress properly a bad code.
In this example i just wanna show how much a minifier does.
What you should do before you minify
And regarding jQuery... i don't use jQuery.jQuery is for old browsers,it was made for compatibility reasons .. check caniuse.com, almost everything works on every browser (also ie10 is standardized now) , i think now it's just here to slow down your web application...if you like the
$()
you should create your own simple function.And why bother to compress your code if your clients need to download the 100kb jquery script everythime?how big is your uncompressed code? 5-6kb..? Not to talk about the tons of plugins you add to to make it easier.Original Code
When you write a function you have an idea, start to write stuff and sometimes you end up with something like the following code.The code works.Now most people stop thinking and add this to a minifier and publish it.
Here iss the minified code (i added the new lines)
Minified using (http://javascript-minifier.com/)
But are all those vars , ifs, loops & definitions necessary?
Most of the time NO !
OPTIONAL (increases the performance & shorter code)
Math
)while
,for
... notforEach
)"{}","()",";",spaces,newlines
Now if a minifier can compress the code your doing it wrong.
No minifier can compress properly a bad code.
DIY
It does exactly the same thing as the codes above.
Performance
http://jsperf.com/diyminify
You always need to think what you need:
Before you say "Noone would write code like the one below" go and check the first 10 questions in here ...
Here are some common examples i see every ten minutes.
Want a reusable condition
Alert yes only if it exists
Alert yes or no
Convert a number to a string or viceversa
Round a number
Floor a number
switch case
try catch
more if
but
indexOf
is slow read this https://stackoverflow.com/a/30335438/2450730numbers
Some nice articles/sites i found about bitwise/shorthand:
http://mudcu.be/journal/2011/11/bitwise-gems-and-other-optimizations/
http://www.140byt.es/
http://www.jquery4u.com/javascript/shorthand-javascript-techniques/
There are also many jsperf sites showing the performance of shorthand & bitwsie if you search with your favorite searchengine.
I could go one for hours.. but i think it's enough for now.
if you have some questions just ask.
And remember
No minifier can compress properly a bad code.
You could use one of the many available javascript minifiers.