Cache jQuery objects to improve performance
JQuery makes the choice of elements very simple, which is also a major reason for its rapid popularity. If you look at the code that was just written by jQuery friends, the jQuery selector will be abused in most code writing.
If
1. always inherit from the ID SelectorThe fastest selector in jQuery is the ID selector because it comes directly from the getElementById () method of JavaScript.For example, there is an HTML code:Copy codeThe Code is as follows:If the following selector is used, the efficiency is inefficient.Var traffic_button = $ ("# content. button ");Because the button already has an ID, we can directly use the ID selector. As follows:Var traffic_button = $ ("# tr
The cause of the problem: Responsible for the company's Development platform research and development, consider the protection of intellectual property, must consider the Java encryption technology and JS script encryption technology. In the current Java encryption is easy to crack the case, or the first to get JS encryption and compression, on the one hand can improve the performance of the page load, on the other hand also hope that the results of h
For special effect performance, which of canvas, css3, and jquery is more suitable for mobile phones? Recently, I used dom + jquery to create a mobile application with 6 pages, some of which have effects such as gear rotation and the running of stars. After doing well, I found that the performance was really poor, then
'); Ditto
$ ('. Gender Input:radio '); It's so much better.
Using Event delegation
Event delegation allows you to bind an event handler for a container element (for example, an unordered table) without having to bind each element within the container (for example, a list item). jquery provides $.fn.live and $.fn.delegate. If possible, you should use $.fn.delegate instead of $.fn.live, because it eliminates the need for unnecessary select
Document directory
1. Use the latest version.
2. Merge and minimize scripts
3. Replace each with
4. Replace the class selector with ID
5. Specify the front and back text for the selector
6. Create a cache
7. Avoid Dom operations
8. Avoid using Concat () and use join () to process long strings.
9. Return the value of false.
10. Additional tips-cheat sheet and reference documents
This article provides ten steps to immediately improve the performa
jquery makes the selection of elements incredibly simple, which is one of the big reasons for its rapid popularity, and if you look at the code that you've just started using jquery friends, you'll find that most of the code you write is abusing the jquery selector. If you find that the same element is being looked up multiple times, you should cache the
Do seven things to help you improve the performance of jquery, do you want to know a few things?
1. Append Outside of Loops
Anything that touches the DOM comes with a price. If you add a lot of elements to the DOM, you'll want to attach them all at once, rather than doing them multiple times. A common problem arises when additional elements are added to the loop.
$.each (myarray, function (I, item
);
Another simple technique is to build a string continuously in each iteration of a loop. After the loop ends, set the HTML of the DOM element to this string.
The Code is as follows:
Var myHtml = "";
$. Each (myArray, function (I, item ){
MyHtml + = "
});
$ ("# Ballers" ).html (myHtml );
Of course there are other tips for you to try. A site named jsperf provides a good way to test these performances. This website allows you to use every Benchmark Test Technique and visualize its cro
Although JavaScript is much slower than Java C + +, jquery is much slower than native JS, but we can improve the efficiency of code execution by good programming practices.I. Use of selectorsChoose the same element, the performance is different between the various methods, sometimes the difference is particularly large. Usually the more commonly used selectors are the following:ID Selector $ ("#id")Tag Sele
There are a large number of log articles discussing jQuery selectors and their performance impact. As you know, you can use ID, TagName, or ClassName to select elements. Depending on the selector, jQuery uses the local method of the browser, for example, using ID or label to select an element, alternatively, you must manually retrieve the elements from the DOM wh
an element based on the given ID always uses # ID to find the elementThe Code is as follows:// Very fast$ ('# Container Div. robotarm ');// Super fast$ ('# Iner '). find ('div. robotarm '); use $. FN. the find method is faster, because the first selector does not need to be processed by the selector engine. The fastest selector in jquery is the ID selector. because it is directly from the getelementbyid () method of JavaScript, It is very fast becaus
This article is a performance test of jquery's various show/hide methods. The authors tested the phrase from Robert Duffy's jquery conference at San Francisco: ". Hide () and. Show () are slower to execute than direct changes to CSS." But the author did the test by failing to find Robert Duffy to ask why. The following translation is not a full text translation, only a few points.
Used as a test is an HTML
'). click (PI. candyMtn );$ ('# Happiness'). load (url + '# unicorns', PI. unicornCb );},CandyMtn: function (e ){$ ('# Yayeffects'). slideUp (PI. slideCb );},SlideCb: function (){...},UnicornCb: function (){...}}$ (Document). ready (PI. onReady );
Optimization SelectorNode Selection and DOM operations, matching an element based on the given ID always uses # id to find the elementCopy codeThe Code is as follows: // very fast$ ('# Container div. robotarm ');// Super fast$ ('# Iner '). find ('div.
);});$( "#ballers" )[ 0 ].appendChild( frag );
Another simple technique is to build a string continuously in each iteration of a loop. After the loop ends, set the HTML of the DOM element to this string.
var myHtml = "";$.each( myArray, function( i, item ) { myHtml += "" + item + "";});$( "#ballers" ).html( myHtml );
Of course there are other tips for you to try. A site named jsperf provides a good way to test these performances. This website allows you to use every Benchmark Test Technique
In fact, looking at the source code of jquery, found that each of the code is very simple, but why the performance and the original for loop dozens of times times the difference?
The core code for each of jquery
for (; i
It looks simple, but why is it so much slower?
Write the test code as follows:
var length=300000;
function Getarr () {var
JQuery is a very good javascript framework. When we use jQuery, we don't want to return to the lengthy javascript code, so jQuery's optimization is in front of us. So what aspects should we start with to optimize JQuery?
1. Use the latest version of jQuery
Compared with the old version, the new version improves
ancestor element id with the shortest path as the default search range and use the pure class selector or tag selector;
3. Avoid complex selector whenever possible
Avoid performing global search
1
$("div.bizHelp");
=>
1
$("#container").find(".bizHelp");
Ensure that the query path is the shortest and the performance is the best. Refer to the first article;
Avoid null results
For the selection result with a qua
This article provides ten steps to immediately improve the performance of your script. Don't worry. This is not an advanced technique. Everyone can use it! These skills include:
Use the latest version
Merge and minimize scripts
Replacing each with
Replace class selector with ID
Specify the front and back text for the selector
Create Cache
Avoid DOM operations
Avoid using concat () and use join () to process long strings
Returns fal
+ ");
}
Console.timeend (' saved ');
Pressing F12 under Chrome to open the console is a comparison of the execution time when the jquery object is not saved and the jquery object is saved, and you will begin to pay attention to the performance of the code through this diagram. A front-end UI framework that can 500% improve development efficiency!When I swap
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.