Forum Moderators: open

Message Too Old, No Replies

If You Use jQuery, Use it for Everything?

Code Size Savings

         

TheMadScientist

3:52 am on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Today I was looking at the size of a .js file for a site I've been working on, and it was mostly 'stock standard' JS, meaning everything was spelled out in the standard format:

document.getElementById('Id_Name_Here').style.backgroundColor='colorHere'
document.getElementById('Id_Name_Here').innerHTML='Stuff Here'

The file is fairly good sized (19.4k uncompressed), and I wanted to shorten it, but needed all the functions... I was using jQuery for some of the animations and effects it creates, but most of the code was standard JS, so today I spent most of the afternoon converting it to jQuery format and saved a 1.6K on the file size, mainly with the change in formatting.

Everything still does the same thing and it does it the same way, but the code is so much shorter it's not even funny... Prior to converting to jQuery it was 19.4k after the conversion it is 17.8k.

jQuery Format:
$('#Id_Name_Here').css('background-color','colorHere')
$('#Id_Name_Here').html('Stuff Here')

Another thing I did with the animation and often used stylings is set variables for them... On a small file without too much animation or repetition it might not make too much difference, but on larger files with a bunch of interaction it definitely helps to save characters anywhere you can. I changed 'slow' to 'var s' for the animation and one of the biggest savings I got with a single change was from background-color or backgroundColor to bcg

var s='slow'
$('#Id_Name_Here').slideDown(s)

Is the same as:
$('#Id_Name_Here').slideDown('slow')

And
var bgc='background-color'
$('#Id_Name_Here').css(bgc,'colorHere')

Is the same as:
$('#Id_Name_Here').css('background-color','colorHere')


Anyway, I usually do things like 'condensing' with PHP and HTML, but haven't really ever 'condensed' JS to the extreme I did today, and I was really surprised at how many characters I cut out of the code that were totally unnecessary to do exactly the same thing mostly by simply changing the format of the code to jQuery, and I figured I'd post about it for a minute.

coopster

11:08 pm on May 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Don't forget you always have to take the size of the JavaScript library into account too. The current release of jQuery comes in at 24K (compressed).

I keep a source copy of all my JS and use the Yahoo compression utility [developer.yahoo.com] to compress my own software prior to live release. Use the minified version in production and the source for troubleshooting and development. You'll be amazed at how much the compression helps.

TheMadScientist

11:30 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Good point Coopster...

The size of the Library was one of the things that made me take a long look at the script size. I'm personally using JQ 1.3.2 and it's 19.7k minified and g-zipped. I figured with including a library that big I needed to eliminate as many characters as I could from my scripts to keep the overall size somewhat reasonable.

I've actually got mine down to 17.1 now and it's only weighing in at 3.6 compressed, so combined the scripts I'm running are not as big as the most recent version of jQuery even.

I can actually eliminate a couple more k off the top by removing the line breaks and white space like you're talking about... I ran a test on it last night and went from 17.8 to 15.4 just by removing the white space.

One of the other things I've been doing is shortening variable names and leaving a 'key' commented into the working version, which is how I got rid of another .7k today.

All those descriptive variable names are real nice and look pretty, but they sure take up quite a bit of unnecessary space, because it doesn't really matter to the browser, bandwidth, or even end user if they're easy to read and understand or not...

Extensive (any) comments in a live version? They're out too.

coopster

11:49 pm on May 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The compression utility will do all that for you too. You can leave your field names as descriptive as you would like, throw in whitespace where necessary, and add loads and loads of comments so the next time you have to work with the n-thousand-plus lines of JavaScript code you wrote it will all make sense once again without having to relearn what you wrote. I remember when I first went out looking for a good compression utility I was most impressed by the work completed by this engineer at Yahoo. I ran it against a smaller .js file so I could see how it worked and it was pretty nifty.

TheMadScientist

11:55 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Cool... I didn't read that it tokenizes everything it can.
Thanks for the link and pointing it out.