10 November 2009 0 Comments

jQuery Optimization : Avoid Unnecessary Selector

Common subexpression elimination is a common way to optimize any programming code. Doing unnecessary selector is expensive. Doing something like this:

jQuery('.class').each(function(){
	jQuery(this).html();
	jQuery(this).find('div').each(function(){
		//etc.
	});
});

which required many selector it is best to use 1 instead since we are repeating ourselves and doing some redundant selector.

jQuery('.class').each(function(){
	var obj = jQuery(this);
	obj.html();
	obj.find('div').each(function(){
		//etc.
	});
});

This is something that we often see in many plugin. Many plugin contains unnecessary selector which degrade the perform of the plugin little by little.

http://jqueryblog.net/wp-content/plugins/sociofluid/images/digg_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/reddit_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/dzone_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/delicious_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/blinklist_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/blogmarks_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/furl_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/newsvine_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/technorati_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/magnolia_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/google_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/myspace_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/facebook_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/yahoobuzz_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/sphinn_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/mixx_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/twitter_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/jamespot_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/meneame_24.png

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes