Other techniques for DHTML Performance Improvement
Truly
More performance tips)
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dndude/html/dude100499.asp
One of the most important tasks in Microsoft's development process is to optimize the product.
Most developers regard Performance Tuning as part of a functional review. For Win32ProgramOptimization has been carried out for many years, and there are a lot of literature for reference.
The headache for many DHTML and HTML developers is that there is not enough technical literature to learn how to make pages faster and why pages are slow. Of course, there are some simple things, such as not using 2 MB images on the page. However, in our work on DHTML page acceleration, we found some interesting tips and tricks that can be used to improve the performance of your own pages.
What I want to adjust this timeCodeThe example is a Table Builder. It constructs a 1000-row table by using the document. createelement () and element. insertbefore () methods. Each row contains a cell. The content of each cell is "text ". What is the worst code to implement this code? How much can this optimization code be improved? A lot!
At first I wrote a piece of code that I thought was very fast. I tried to avoid some very low-level errors, such as not declaring variables, or using VBScript and JScript on the same page at the same time. The Code is as follows:
<HTML> <body> <SCRIPT> var TBL, tbody, TR, TD, text, I, Max; max = 1000; TBL = document. createelement ("table"); TBL. border = "1"; tbody = document. createelement ("tbody"); TBL. insertbefore (tbody, null); document. body. insertbefore (TBL, null); for (I = 0; I <Max; I ++) {TR = document. createelement ("TR"); TD = document. createelement ("TD"); text = document. createtextnode ("text"); TD. insertbefore (text, null); tr. insertbefore (TD, null); tbody. insertbefore (TR, null) ;}</SCRIPT> </body>
View Example 1
All tests run on Intel Pentium II 233, 64 MB memory, Windows NT 4.0, and Internet Explorer 5. Each page is loaded from a local disk. The timing of all events is from the initial loading of the page to the page "static" (all queue events have been executed, and the screen is re-painted ). The baseline page (I call it 'test1') takes 2328 milliseconds.
DHTML pages are often used and time-consuming operations are global object references. Such as "document", "body", and "window", these variables are much more expensive to reference than simply referencing a local variable.
I decided to change theDocument. BodyReference "cache" to a variable "thebody ". It only adds another reference to the body attribute, so this change is relatively small.
This line is added to the Code:
VaR thebody = Document. Body;
And line:
Document. Body. insertbefore (TBL, null );
Adjusted:
Thebody. insertbefore (TBL, null );
View Example 2
The above adjustment does not affect the entire time, and only saves 3 ms of loading time. However, if we introduce this code into the main loop, the results will become very meaningful.
Next, I plan to cache the document object itself. The document object is referenced for 3002 times in this example.
The following is the version after the document is cached to the local variable. All other references also point to the local variable:
<HTML> <body> <SCRIPT> var TBL, tbody, TR, TD, text, I, Max; max = 1000; var thedoc = Document; var thebody = thedoc. body; TBL = thedoc. createelement ("table"); TBL. border = "1"; tbody = thedoc. createelement ("tbody"); TBL. insertbefore (tbody, null); thebody. insertbefore (TBL, null); for (I = 0; I <Max; I ++) {TR = thedoc. createelement ("TR"); TD = thedoc. createelement ("TD"); text = thedoc. createtextnode ("text"); TD. insertbefore (text, null); tr. insertbefore (TD, null); tbody. insertbefore (TR, null) ;}</SCRIPT> </body>
View Example 3
In this version, page loading only takes 2100 Ms. In this way, we have reduced the entire running time of the application by 10%, and it is very practical. It takes about 4 ms to obtain a document variable than a local variable.
Whether a commonly used document loading speed is optimized by specifying the defer attribute for the <SCRIPT> label. This attribute is only applicable when you do not need to run the code in <SCRIPT> immediately. (The code that runs immediately is not in the function body-the code will be executed immediately after the script block is loaded.) When the defer attribute is set, ie will not wait to load and convert the script. This also makes page loading much faster. This usually means that the script to be run immediately should be encapsulated in a function, andOnloadEvent processing. It is more useful if your script depends on user actions after page loading, such as clicking a button or moving the mouse to a certain area! However, if your code needs to be executed immediately or immediately after the page is loaded, you cannot use this feature.
The following is a new version with the defer attribute added.
<HTML> <body onload = "Init ()"> <SCRIPT defer> function Init () {var TBL, tbody, TR, TD, text, I, Max; max = 1000; var thedoc = Document; var thebody = thedoc. body; TBL = thedoc. createelement ("table"); TBL. border = "1"; tbody = thedoc. createelement ("tbody"); TBL. insertbefore (tbody, null); thebody. insertbefore (TBL, null); for (I = 0; I <Max; I ++) {TR = thedoc. createelement ("TR"); TD = thedoc. createelement ("TD"); text = thedoc. createtextnode ("text"); TD. insertbefore (text, null); tr. insertbefore (TD, null); tbody. insertbefore (TR, null) ;}</SCRIPT> </body>
View Example 4
The page loading time is reduced to 2043ms. The baseline page is 12% higher than the previous version.
The next step is more meaningful, but it is a bit tricky. When creating elements and adding them to the node tree, it is more efficient to add them to the main document-instead of adding them to a large subtree, then add the subtree to the document. For example, if we want to create a new row in the table with a cell containing text, I will do this:
Create <tr>
Create <TD>
CreateTextnode
InsertTextnodeInto <TD>
Insert <TD> into <tr>
Insert <tr> into tbody
However, the above method will be slower than the following:
Create <tr>
Create <TD>
Create textnode
Insert <tr> into tbody
Insert <TD> into <tr>
InsertTextnodeInto <TD>
The first method is used in the preceding example. Example 5 uses the second method. The complete code is as follows:
<HTML> <body onload = "Init ()"> <SCRIPT defer> function Init () {var TBL, tbody, TR, TD, text, I, Max; max = 1000; var thedoc = Document; var thebody = thedoc. body; TBL = thedoc. createelement ("table"); TBL. border = "1"; tbody = thedoc. createelement ("tbody"); TBL. insertbefore (tbody, null); thebody. insertbefore (TBL, null); for (I = 0; I <Max; I ++) {TR = thedoc. createelement ("TR"); TD = thedoc. createelement ("TD"); text = thedoc. createtextnode ("text"); tbody. insertbefore (TR, null); tr. insertbefore (TD, null); TD. insertbefore (text, null) ;}</SCRIPT> </body>
View Example 5
Example 5 only takes 1649 Ms. It is 25% faster than the previous version, and almost 30% faster than the baseline page.
The next step is to modify the table usage.Fixed-tableLayout ). If the column width of the table after the fixed-table layout is specified, use the <Col> label setting. Alternatively, if the <Col> label is not specified, the space of each cell is evenly divided by row. The fixed-table layout style improves table performance because the size of the content of each cell does not need to be calculated. This is a very practical way to improve performance, especially for large tables with many columns.
This operation can also be implemented by simply adding CSS styles:
TBL. style. tablelayout = "fixed ";
View Example 6
However, since this example only has one cell, this change only improves the performance by 1.6%. This prompt increases with the number of cells in the table.
The last two examples include adjusting the method of assigning values to cells. In all examples,TextnodeAnd add it to TD. In Example 7InertextInstead of inserting a text node, the code is adjusted:
TD. innertext = "text ";
(TRANSLATOR: innertext is only supported in IE and is an IE extension. It is compatible with Firefox and can use innerhtml)
View Example 7
Surprisingly, this change has a significant effect-9% faster than the previous one, with a total increase of 36%. In this way, our final test replaced 2323 ms of the baseline page with 1473ms.
Almost everyone knows the current location.Element. innerhtmlIt is very slow, very slow. I want to know how slow it is.InnerhtmlReplaceInnertextTo add text to cells. This adjustment hits performance, and the total time becomes 3375 MS, which is 80% worse than the previous version !. This change is 45% less than the baseline page. Obviously,InnerhtmlIs quite expensive.
You can view all the test results, showing the drill and average values.
Optimizing HTML pages is the same as tuning Win32 applications. You must know where to slow down and where to do it quickly. Hope these skills can help you speed up your page.