Jquery tips (4) Some tips for improving jquery Performance

Source: Internet
Author: User

It is best to start with an ID selector when selecting

I think this is easy to understand, because jquery uses document. the getelementbyid method performs ID selection. This method is faster than all other dom selection methods, so it is best to start with $ ("#"), for example:CopyCodeThe Code is as follows: <Div id = "A">
<Div class = "B">
<Div class = "C">
<Div class = "D"> </div>
</Div>
</Div>
</Div>
<SCRIPT type = "text/JavaScript">
$ (". B. C. D") // slow one
$ ("# A. B. C. D") // fast one
</SCRIPT>

Context of $ ()
When you use $ () to select page elements, provide the selected range to reduce the selection time. In other words, filtering the selector within a small area of the page instead of the entire page reduces the filtering time. This can be achieved by providing the second parameter in the $ () function as the context.Copy codeThe Code is as follows: <Div id = "test">
<Div class = "inner"> Hi </div>
</Div>
<SCRIPT type = "text/JavaScript">
Alert ($ (". Inner", document. getelementbyid ("test"). Text (); // increase the speed by provide context
Alert ($ (". Inner"). Text (); // traverse all the element so that is slower than abve
</SCRIPT>

Of course, in jquery-defined (or JS function) events, you can use this to refer to the context:Copy codeThe Code is as follows: <Div id = "test">
<Div class = "inner"> Hi </div>
</Div>
<SCRIPT type = "text/JavaScript">
$ ("# Test"). Click (function (){
VaR text = $ (". Inner", this). Text (); // This means $ ("# test ")
Alert (text); // alert hi
});
</SCRIPT>

Of course, the above example can also be written in the following two ways:Copy codeThe Code is as follows: <Div id = "test">
<Div class = "inner"> Hi </div>
</Div>
<SCRIPT type = "text/JavaScript">
Alert ($ ("# test. Inner"). Text (); // method 1
Alert ($ ("# test"). Find (". Inner"). Text (); // method 2 and it was best one
</SCRIPT>

The use of the find method is the most efficient among all methods.

Of course, if you use the ID selector, that is, $ ("#..."), you do not need to provide context parameters. This does not affect the speed.

Save frequently-used elements encapsulated by jquery
This is important, for example, because it takes time to select page elements by using $ (). Saving it as a variable can avoid this waste. For example:Copy codeThe Code is as follows: <ul>
<Li> one </LI>
<Li> two </LI>
<Li> three </LI>
<Li> four </LI>
<Li> five </LI>
</Ul>
<SCRIPT type = "text/JavaScript">
For (I = 0; I <$ ("Ul li "). length; I ++) {// very bad, select $ ("Ul li") so many times, waste a lot of time
Alert ($ ("Ul li") [I]. innerhtml); // same here, very bad
}
VaR $ li = $ ("Ul li ");
For (I = 0; I <$ Li. length; I ++) {// good one, only selct $ ("Ul li") once
Alert ($ Li [I]. innerhtml); // same here, good
}
</SCRIPT>

As you can see from the code, avoiding repeated selections can improve performance :-)

Use as few selector as possible
Jquery's selector is array-oriented, so use as few as possible when conditions permit, such:Copy codeThe Code is as follows: <Div id = "div0"> </div>
<Div id = "div1"> </div>
<Div id = "div2"> </div>
<SCRIPT type = "text/JavaScript">
$ ("# Div0"). slidedown ("slow ");
$ ("# Div1"). slidedown ("slow ");
$ ("# Div2"). slidedown ("slow"); // slow

$ ("Div0, div1, div2"). slidedown ("slow"); // fast
</SCRIPT>

It can be seen that using the selector and separating the selected elements with commas, and selecting multiple elements not only makes the code more concise, but also improves the performance by reducing the number of jquery instances!

Avoid using $ (). Each when there are many cycles, and use for Loop
Use $ (). the each method makes programming easier when looping, while a small number of loops are using $ (). the effect of each on performance is negligible. However, when this number is large, the impact on performance starts to become significant.

This number. I checked the information. It is said that the $ (). Each method can be used below 1000. If this number continues to increase, the for loop statement should be used.

Minimize Dom operations
Dom operations are relatively expensive on the page (for example, inserting or deleting a piece of text on the page). Minimizing this change is the best practice to maintain performance! For example:Copy codeThe Code is as follows: <ul id = "test">
</Ul>
<SCRIPT type = "text/JavaScript">
VaR $ list = $ ("# test ");
For (I = 1; I <101; I ++ ){
$ List. append ("<li> item" + I + "</LI> ");
} // Very bad, change Dom 100 times

VaR listitem = "";
For (j = 1; j <101; j ++ ){
Listitem + = "<li> item" + J + "</LI> ";
}
List.html (listitem );
// Good practice, only modify Dom once

</SCRIPT>

We can see that the first example modifies the DOM 100 times, while the second example only modifies the DOM once. The above performance gap is obvious.

Jquery animation effects can be shielded.
In some cases, if jquery animation can be disabled, the performance can be improved. The blocking method is as follows:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Jquery. FX. Off = true;
</SCRIPT>

If the parameter can be a JS object, try to use the object
For jquery plug-ins, or jquery's CSS and ATTR methods both accept key/value or JS key/value object pairs as parameters, passing key-value objects can reduce the creation of jquery objects, such:Copy codeThe Code is as follows: <div> </div>
<SCRIPT type = "text/JavaScript">
$ ("Div" ).css ("display", "Block ");
$ ("Div" ).css ("background-color", "blue ")
// Slow, because it create more jquery object

$ ("Div" ).css ({"display": "Block", "background-color": "blue "});
// Fast, only create one object
</SCRIPT>

You can also use the concatenation method:Copy codeThe Code is as follows: <div> </div>
<SCRIPT type = "text/JavaScript">
$ ("Div" ).css ("display", "Block" ).css ("background-color", "blue ");

</SCRIPT>

However, the performance of this method is not as good as the above. Two methods are required and temporary objects need to be generated.

The above are some small tips for jquery Performance Improvement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.