views:

985

answers:

11

I was surfing the web, as usual and I wanted to take a look at Bing's source code because I was curious and wanted to know what they were using to make their image fade in. I was surprised by what could only be qualified by the following line :

Holy wall of text!

And it made me wonder if there is any advantage to obfuscate your (X)HTML, CSS and JavaScript like this? To reduce the size of the file perhaps?


Edit : After looking around, I realized this was a pretty common practice. Never looked at it from a minification point of view before!

+1  A: 

Yes, file size reduction.

mgroves
+4  A: 

File size would certain be a benefit, so much that for example Gmail itself not only does that but also zip its contents.

Otávio Décio
+7  A: 

2 main reasons to obfuscate your code:

  • Reduce the total size of the file (like you mentioned) If you replace a function called 'CallThisFunctionAndReturnAnArray' with 'C' - you've saved a lot of characters in your file.

  • Protect intellectual property - while if this is realistic is definite debatable, this is common justification for doing so. You can get around this with good tools, or if you are just really smart.

Adam
It's not debatable. Obfuscating javascript is a waste of time. If you're Javascript is interesting, someone WILL reverse-engineer. It's not that difficult.
Triptych
+29  A: 

They are not obfuscating. They are minifying in order to reduce the bandwidth used by the millions of requests they see each day.

The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from production web servers is gzipped as part of the HTTP protocol).

You might also be interested in reading the Yahoo! User Interface blog post titled "Minification v. Obfuscation".

William Brendel
In case this is the selected answer, it's worth noting Peter's [http://stackoverflow.com/users/131858/peter] answer [http://stackoverflow.com/questions/1070514/should-you-obfuscate-your-javascript/1070535#1070535] that one should not attempt to *write code* in such a manner, but this should be done as some type of automated process, or as part of moving the code into Production.
anonymous coward
@anonymous: +1 agreed
William Brendel
This is not answering the question whatsoever. While it is a good answer to a question like 'What is MS doing to their Bing javascript?' it does nothing to answer 'Should you obfuscate your javascript?'
Adam
@Adam: The title of the question is not quite accurate. My answer speaks to what the author actually meant to ask. The author was wondering why Microsoft would obfuscate their Javascript code. I pointed out that they are not obfuscating their code intentionally, but rather minifying for the sake of speed. I think that answers his question adequately.
William Brendel
I've updated the title to more accurately reflect the question and this accepted answer. Hope that helps...
Shog9
Nice, makes more sense now :-)
William Brendel
In my experience, minifying hardly reduces the size at all, and if you're using Gzip (which compresses the size much more), the difference between `normal+gzipped` and `minified+gzipped` is negligible.
DisgruntledGoat
@DisgruntledGoat: If you're Google or Microsoft, it still adds up. For "normal" websites, gzip is usually plenty, but according to the article I linked, minifying+gzip showed a 30% reduction in size compared to gzip alone. I wouldn't call that negligible.
William Brendel
+1  A: 

Yes. Reducing file size not only decreases the download time but also the parse time, which is very useful on some browsers (particularly the devil-spawned IE6).

This probably wants retagging minification.

Alun Harford
+2  A: 

There is absolutely an advantage to this. There is something important to note, though. There is no reason to actually code like this. Rather, after you are finished doing something (and writing in good readable form), use a minifier to remove unneeded white-space and such. For larger files this can greatly decrease load times.

Peter
The mere thought of coding like this gives me chills!
Gab Royer
+2  A: 

I will answer that "obfuscation" is not the goal in these cases, but bandwidth. If you are serving cacheable content at such a rate that reducing the homepage size would be beneficial, then it does make sense to do so.

As for obfuscation to steer away prying eyes - No. Don't do that.

anonymous coward
+1  A: 

I would draw a difference between optimization to reduce file size and obfuscation with the intended purpose of preventing (or at least making hard) reverse engineering. Also, google for example has a "compiler" that extends JavaScript to include some advanced type system and the like; and this extended code is then "compiled" into regular JavaScript. Obviously it makes sense to create this generated code in an optimized manner -- there's no need to make it maintainable.

balpha
+1  A: 

I would bet the main reason they do this is to reduce the file size. Smaller files makes the page load faster.

Obfuscating is not as important. Also, it can make things a pain to debug, depending on how you do it.

Speed is everything in search.

BaroqueBobcat
+1  A: 

And it made me wonder if there is any advantage to obfuscate your (X)HTML, CSS and Javascript like this? To reduce the size of the file perhaps?

Obfuscation is slightly different from another common technique with Javascript files, minification. Minification is common with Javascript since it reduces the file size, which, with large files can significantly reduce their size (since large Javascript files often contain lots of comments, line breaks, unneeded spaces and so forth which can be stripped to reduce the size of the script).

Obfuscation, on the other hand, is also more about concealing your code from others, by changing variable names to others which are obscure, or that remove meaning from the Javascript files. However, by doing this it often reduces the file size, since variable names are often much shorter than those in the original file. This is a common technique to make it harder for others to simply copy code, and is more about protecting the code which is obviously much more easily accessible compared to other platforms.

Doing both minification and obfuscation are advantageous to your scripts, since they reduce the size of your Javascript files, which reduces the time taken to load your pages, and also makes it harder for others to reverse engineer it (if this is an issue for your code).

However: note that it's not foolproof - there are certain tools that exist which make it easier to reverse engineer obfuscated code, so it doesn't make it 100% safe from others who really want to take a look at how your scripts work.

Perspx
+2  A: 

Only to reduce size, and only on large modules. A much greater gain can be achieved using gzip.

If you use any third party code, you may be required to leave their copyright/license comments intact when you minify. People often forget to do this. You'll probably want to leave your own copyright comments intact too.

David