Which selector is faster?

If you have any concerns about performance of your web application, you probably should read this. Some times ago i read on a site (don’t remember which, was like AGES ago) that a good performance improvement is to be VERY specific when you use a library to select an element from the DOM. That article was written with Prototype in mind but i’ve though that will work same for jQuery as well. HUGE MISTAKE!

Kinda obviously, jQuery is NOT Prototype and they seems to NOT work in the same manner. Today i think to test the results of various selector. For this, i used Profiler from Firebug (what? you don’t know what firebug is?). And i was amazed. And i mean AMAZED!

I made this test right on this blog. I used jQuery('li') in console and, after that i used jQuery('ul li') (there is 24 LI elements on the first page). Until today i think the second option is faster. Why? Well.. Is more specific, isn’t it?

The second pick is around 2.6 times SLOWER than first one. (0.533ms vs 0.205ms).

Unfortunately, i’ve try to use the slower technique on many projects. But, lucky me, for no reason, i always forgot to do that (and i use to remember too late) :D

So, be careful! Don’t make this mistake ;) If somebody will tell ya that a method is faster, DON’T take his word as a law. TEST IT!

One development trick

Actually… There is more than one :D

Keep only one copy of your favorite javascript library (and also up to date!)

I use jQuery a lot. No, actually i LOVE jQuery (is small, is fast, is easy to use, you know, usual craps that make you to love it; just like a woman :D ). To avoid duplicates of jquery, i use a folder called assets (but you can rename as you want as well). Here i keep latest version of jquery and jquery UI 1.5.2 (which now is the last stable version). With wget for windows in the assets folder, i wrote a small script to get latest version:

del jquery-latest.min.js
wget http://code.jquery.com/jquery-latest.min.js

Put this in a file called update.jquery.bat and double click it whenever you want to update your version of jquery (or any other library). This way you can keep all your sites up to date :D BE CAREFUL! Some plugins may WON’T work on newer versions of jquery!

Use snippets like a smart guy!

If you use E-texteditor (and if you don’t, then YOU SHOULD!), you probably know what snippets are. But, the question is: are you using them?

I put some snippets all together into an archive HERE. What you should do with this? Go to Start-> Run (or press WIN+R) and write this: %appdata%\e\Bundles then press enter. Extract the archive content right there and…. Done.

In each HTML document you can write ui then press tab. VOILA! A menu with jquery UI components are displayed and waiting for you to pick from there! Ofcourse, you MUST have UI files (minified) in /assets/ui folder (which must be in the root of your project folder). Again, be careful when you update UI. Even though UI 1.6 have same files as UI 1.5.2, there is (still) some issues with 1.6 (actually it’s a beta).

Internet Explorer List margin bug

In some (weird) cases, Internet Explorer (both 6 and 7, i didn’t use 8 yet) add an extra margin to your list elements (LI). This is very annoying and frustrating if you don’t know how to get rid of it.

You have two solution:

First one, if you already coded your site and you saw the bug very lately, you can add a negative margin to list elements (either top or bottom). Every time i used this method i had no problems, so… I think it works :D

Second one implies to use some non-semantic code. Actually… Is not always non-semantic. Sounds weird? Well… Did you ever heard of Definition Lists? DL/DT/DD. If yes, then start using it! IE doesn’t have any problems at all with this. If no, read some infos about this and then start using.

Why i tell you that is both semantic and non semantic code? For instance, on a menu, is more adequate to use UL/LI tags. Is a list? Ofcourse! But, you can use, as well DL/DD tags.

I know, is messy, but it works flawless :D

jQuery AJAX trick (very useful!)

Did you ever ask yourself how can you display a “loading” indicator when you make ajax calls using various libraries?

The simple but efficient answer is this:

$(document).ready(function(){
$('
Loading...
') .ajaxStart(function() {$(this).show();}) .ajaxStop(function() {$(this).hide();}) .appendTo('body'); });

And that’s all. You need to do this only once.

Ofcourse, you can apply some CSS styles to #busy div, using an image generated with ajaxload or something similar.

I didn’t use any other libraries (actually i used prototype with scriptaculous sometime like AGES ago), but i think all cool libraries (like jQuery) have similar methods.

Another great news: with MORE than 50 fixes (but no new features tough) jQuery UI 1.5.1 is out.

Best way to CSS hacks

Every time you code a layout, you must dealing with inconsistencies of various browsers. Due to his lack of standard support, IE (and especially IE6) has a LOT of css hacks available “on the market”. And that because the IE developer team has some kind of humor for two reason:

  1. They know that their browser suck so they put conditional comments. Is not a really hack, because you have a documentation, but is the best (and also the safest) way to fix CSS bugs (or even add some extra content for certain version of IE).
  2. The second reason is funny and creepy: the fresh SP3 for windows is delivered with… IE6. Yes, if you don’t have IE7 installed, you will have IE6. That sucks, isn’t it?

Ok, enough with this, let’s show some real hacks, right?

Internet Explorer hacks

First of all: the best way is to use conditional hacks for IE. That’s because new version of other browsers can misunderstanding some hacks. As far as i know, this wasn’t happen yet, but you never know what gift we can get, right? Use hacks when you have only a couple to fixes!

The most used hack for IE6 is the star selector :

#wrap {border:2px solid blue}
* html #wrap {border:5px solid red}

For IE6, wrap container will have a 5 px border. For any other browser, you will get a 2px blue border. Is important to get IE6 hack AFTER the normal rules, otherwise you won’t get any result.

html>body #wrap {background:white}
#wrap {background:red}

This one is some kind of first rule, but reverted. That’s because IE6 simply don’t know how to deal with this kind of selectors (which is valid tough!). Lucky us, right?

Another hack is the underlined and !important. However, those hacks are NOT recommended for mass production (is ok for testing, but… that’s all):

#wrap {
color:red !important;
color:blue
}

and

#wrap {
color:red;
_color:blue
}

With both hacks you will achieve same result. But i repeat, DO NOT USE it unless you know what are doing. You will avoid a LOT of headaches ;)

Until now, i talk about IE6 hacks. Here you have a IE7 hack:

#header { font-size:1em }
*:first-child+html #header { font-size:2em }

Like i said before, is mandatory that hack to be AFTER proper rule!

IE6 min/max-width hacks

#wrap {
	width: expression(document.body.clientWidth < 742? "740px" : document.body.clientWidth > 1202? "1200px" : "auto");
	}

For this one, we need to use some CSS expressions. Also, is not recommended (can affect user experience, because browser execute that expression). This hack was founded here.

Image flicker problem

One of the most annoying thing on IE is this: when you have a css image on a link, when you hover that link, the image is reloaded, resulting a short time flicker. Mister-pixel.com comes with a handy solution for this (that works only in IE6 SP1 or greater):


You only need to put these lines in your header (inside of conditional tags) and your problem is done.

There is more?

Well… I’m sure i didn’t cover all CSS hacks. However, these are some of the most important hacks, ready to use anytime you need it (and for your own good, you should bookmark this page because you will need it A LOT!). Do you know any other hacks?