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?



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

Answer / 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

More jQuery Interview Questions

What is the difference between .empty(), .remove() and .detach() methods in jQuery?

1 Answers  


How can jquery library be added to pages?

0 Answers  


Is it possible to get value of multiple CSS properties in single statement?

1 Answers  


How to set jquery mobile responsive table width to 100% width despite css styles? : jquery mobile

0 Answers  


What is the method used to define the specific character in place of $ sign?

0 Answers  


How do you select element by ID in jQuery?

1 Answers  


Explain the features of jquery?

0 Answers  


Where jquery mobile works? : jquery mobile

0 Answers  


What were the biggest challenges of getting the 1.7 release out there?

0 Answers  


How to include jquery library in asp.net project?

0 Answers  


How to add or remove classes to an element in jquery?

0 Answers  


Is jquery code exicuted on the clientside, or serverside?

0 Answers  


Categories