Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How does caching helps and how to use caching in jQuery?

Answer Posted / chaitanya

Caching is an area which can give you awesome performance, if used properly and at the right place. While using jQuery, you should also think about caching. For example, if you are using any element in jQuery more than one time, then you must cache it. See below code.

Hide Copy Code

$("#myID").css("color", "red");

//Doing some other stuff......

$("#myID").text("Error occurred!");



Now in above jQuery code, the element with #myID is used twice but without caching. So both the times jQuery had to traverse through DOM and get the element. But if you have saved this in a variable then you just need to reference the variable. So the better way would be,

Hide Copy Code

var $myElement = $("#myID").css("color", "red");

//Doing some other stuff......

$myElement.text("Error occurred!");



So now in this case, jQuery won't need to traverse through the whole DOM tree when it is used second time. So in jQuery, Caching is like saving the jQuery selector in a variable. And using the variable reference when required instead of searching through DOM again.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can we apply css in last child of parent using jquery library?

807


Why content injected into a page is not enhanced? : jquery mobile

726


How to work with jQuery parent(), children() and siblings()?

851


What is difference between .setinterval() and .delay() ?

767


What is a dom in jquery? How can we make sure that dom is ready?

748


What is the difference between jquery-x.x.x.js and jquery.x.x.x min.js?

784


What is serialize() in jquery?

742


What is .siblings() method in jquery?

789


What are the difference between empty(), remove() and detach() functions in jquery?

841


What is jquery toggle function?

811


How method can be called inside code behind using jQuery?

796


What does jQuery data() function do?

808


Define .siblings() method in jquery?

760


Explain finish method in jquery? : jquery mobile

845


How to programmatically trigger a click event thats being handled by jquery only?

769