There are a lot of Konami Code easter eggs out there and also a lot of guides on how to implement your own. Most of these guides have one thing in common; they are using overly verbose code for something you could fit inside a Twitter tweet.
The last week I’ve seen a dozens of blog posts related to Facebook’s, Jquery’s and many others hidden implementation of the ‘Konami code’ and how to implement one yourself.
One of the things I noticed was that people tend to write very verbose code, meaning, they tend to write a lot more code then they need too. Take a look at Abhi’s 30-rows implementation or Trevor’s Cheat Code Jquery plugin(!).
There are numbers of valid explanations to why people write verbose code; to be overly clear for educational purposes is one. But if you do that, I think you should also teach people that they shouldn’t make a habit out of it, especially when it comes to something so small that it fits inside a Twitter tweet.
// Tweetable Konami code
var k=[];addEventListener("keyup",function(e){ k.push(e.keyCode);if(k.toString().indexOf("38,38,40,40,37,39,37,39,66,65")>=0)cheat()},true);
The code above will fire the function cheat() when you enter the Konami Code (up,up,down,down,left,right,left,right,b,a). The code might be a bit too concies, but I was mainly trying to make a point.
Haven’t seen the Konami Code in action?
Go to www.facebook.com and login. Click once anywhere on your home page and type the following sequence using your keyboard: Up, Up, Down, Down, Left, Right, Left, Right, B, A, Enter Key. Click again or scroll the page.
Slice loading time in half with fewer HTTP requests
Reducing the number of HTTP requests is the key to faster pages. A simple way of doing this is by combining javascripts and stylesheets into single files using Rakaz’s combine script.
According to Yahoos article “Best Practices for Speeding Up Your Web Site” about 80% of the end-user response time is spent on the front-end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page.
One way of reducing the number of HTTP requests while still support rich page designs is to use a server side script that combines all your javascripts and styles into single files. A php script that does exactly that is Rakaz’s combine.php.
When the script is configured and ready to use all you need to change are the urls to your scripts and styles. You will go from doing, in this case, four requests to only one.
<script src="jquery-1.3.2.js" type="text/javascript"></script> <script src="jquery.valid8-1.1.1.js" type="text/javascript"></script> <script src="jquery.defaultvalue-1.0.js" type="text/javascript"></script> <script src="global.js" type="text/javascript"></script>
<script src="jquery-1.3.2.js,jquery.valid8-1.1.1.js,jquery.defaultvalue-1.0.js,global.js" type="text/javascript">
Rakaz’s script will also compress and cache your combined files to reduce loading times even more. You can find a complete installation guide and more information about the script over at rakaz.nl
Real world example
I just uploaded and configured combine.php for http://chat.unwrongest.com. Before the script was added you needed to download 10 javascript files that weighed 232 kb in total. On average this took about 3 seconds to download. After installing the combine.php script you now need to download only a single file of 68 kb which only takes around 1 second to download. The total loading time of chat.unwrongest.com is now, for me, around 2 seconds instead of just above 4 seconds.
How to decompress javascript
The online beautifier is a javascript decompression tool. It takes a compressed javascript and inserts indents, new lines and makes it readable. You find the online beautifier at http://elfz.laacz.lv/beautify/.
When I am finished with a javascript component, like Elastic, Tabify or Accordion, I always create a compressed version of the source code using a javascript compressor like YUI Compressor or Dean Edwards Packer. The compressor shortens long internal variable names and removes unnecessary white spaces making the code lighter.
Developers often only provide these compressed versions of the code, which makes the code hard to read, understand and modify. The online beautifier is a javascript decompressor. It doesn’t get back the original variable names but it does create indentations and new lines. Below is an example of what the online beautifier does with the compressed code of my Jquery plugin Accordion.
This is the compressed and minified version of the jquery plugin Accordion. The whole source code is on one line and all white spaces are removed.
(function($){$.fn.extend({accordion:function(){return this.each(function(){if($(this).data('accordiated'))return false;$.each($(this).find('ul, li>div'),function(){$(this).data('accordiated',true);$(this).hide()});$.each($(this).find('a'),function(){$(this).click(function(e){$(e.target).parent('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp('fast');$(e.target).siblings('ul, div').slideToggle('fast');return void(0)})})})}})})(jQuery);
This is exactly the same code after the online beautifier is done with it. The tabs and new lines it has inserted has made the code a lot more readable.
(function($) {
$.fn.extend({
accordion: function() {
return this.each(function() {
if ($(this).data('accordiated')) return false;
$.each($(this).find('ul, li>div'),
function() {
$(this).data('accordiated', true);
$(this).hide()
});
$.each($(this).find('a'),
function() {
$(this).click(function(e) {
$(e.target).parent('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp('fast');
$(e.target).siblings('ul, div').slideToggle('fast');
return void(0)
})
})
})
}
})
})(jQuery);
The tool is not perfect, and it can’t decompress everything, e.g. it can’t decompress a file which have been Base62 encoded – But I like it and think you should give it a go at http://elfz.laacz.lv/beautify/.
Projects
- Accordion (jQuery)
- Airport (jQuery)
- Chat
- Defaultvalue (jQuery)
- Elastic (jQuery)
- Highlight (jQuery)
- Keycan
- Lazy (jQuery)
- Limit (jQuery)
- Password Strength (jQuery)
- Show Password (jQuery)
- Tabify (jQuery)
- Valid8 (jQuery)
Latest posts
- July 8th, 2009 Why are we typing passwords twice?
- July 5th, 2009 Don’t stop password masking; let the user decide
- June 1st, 2009 You can’t validate email addresses with regular expressions
- May 20th, 2009 Konami Code: Why so verbose, when you can make it in 140 characters?
- May 18th, 2009 Let your users know if Firebug slows down your web page.