Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How does caching helps and how to use caching in jQuery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 231
    Comment on it

    Hello Readers,

    In jquery when we use the two or more than two same id's as a selector or elements and operate over jquery event than sometimes effect on its performance due to caching.

    For Example :

    $("#mydemo").css("color", "red");
    //Doing some other stuff
    $("#mydemo").text("Error occurred");
    

    Now in above jQuery code id #mydemo is used two times. So, both the times jQuery traverse down the DOM tree with the same id #mydemo . So, if you overcome this caching we use to store the selector and its event in a variable and attach this varaible into another event.

    For Example :

    var $myElement = $("#mydemo").css("color", "red");
    //Doing some other stuff
    $myElement.text("Error occurred!");
    

    So,in the above code, jQuery use the id #mydemo with CSS event and store into the myElement variable after that use this varible when we use second event in jquery instead of calling again the same id in jquery.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: