Ways to speed up IE's JavaScript document output _javascript tips

Source: Internet
Author: User
Add the following code to the front of the JavaScript
Copy Code code as follows:

/* @cc_on _d=document;eval (' var document=_d ') @*/

To join such a line of code IE's document access speed can be increased by at least 5 times times more
Here is the test comparison code before and after joining
Copy Code code as follows:

Before
var date = new Date;
for (var i = 0; i < 100000; i++) document;
Alert (new date-date); 643

Copy Code code as follows:

/* @cc_on _d=document;eval (' var document=_d ') @*/

After
date = new Date;
for (var i = 0; i < 100000; i++) document;
Alert (new date-date); 145

Speed up a lot of it!

Explanation:
First of all, ie in the case of the document is directly called the word is executed is the window object of the internal functions, and this relatively inefficient. Depending on this, the following processing can increase the speed:
var doc = document;

Document Slow
Doc This is faster than the above (document)

Although the above can be used to write directly, but the document used before the place to replace, this is a bit of a little trouble. So, look at the following:
var doc = document;
var document = doc;
It would be great if it could be achieved ...

People who know JavaScript should know that JavaScript variables are generated at the very beginning, so the document here becomes undefined.
Never mind, keep on improving ~
var doc = document;
Eval (' var document = Doc ');

The role of Eval is to change variables within scope, so that the subsequent document can be used normally.
Finally, add only the conditions that are valid in IE, just like the following is OK ~
Copy Code code as follows:

/* @cc_on
var doc = document;
Eval (' var document = Doc ');
@*/

Extrapolate, as in the following notation, global variables other than document can also be accelerated by using the above method.
Copy Code code as follows:

/* @cc_on
Eval ((function (props) {
var code = [];
for (var i = 0 L = props.length;i<l;i++) {
var prop = Props[i];
Window[' _ ' +prop]=window[prop];
Code.push (prop+ ' =_ ' +prop)
}
Return ' var ' +code.join (', ');
} (' document self top parent alert SetInterval clearinterval
SetTimeout cleartimeout '. Split ("));
@*/

here is Franky's reply:
First of all, ie in the case of the document is directly called the word is executed is the window object of the internal functions, and this relatively inefficient. Depending on this, the following processing can increase the speed:

This is not true.

The main difference between your tests and your test is the scope chain lookup.
Your code is in the global execution environment. So IE, you will visit the global object to find the member of key ' document '. This object is the host object of a COM + implementation in IE. He is not in global. Not in global, go to the window to find again. Caused the speed to slow down.

The same global object, Math. Will not bring this problem. The reason is that math is on global. Once the scope chain lookup is found.

For optimization. One suggestion is
var win = window, doc = document,undefined;
Within each level of scope, if this member uses more than two times, it is meaningful.

And if you only use the IE condition annotation in the global scope, first of all, no IE will not be able to enjoy the benefits of shortening the scope. Of course, non-IE does not exist global->window one more responsibility chain lookup.

The core of optimization here is to shorten the scope chain. Although the newer versions of opera Chrome Safarai, the scope chain lookup has been optimized. But we think of shortening the scope chain. It has a positive effect on older browsers. And for browsers that are optimized, it does not have a negative impact on the face.
Related Article

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.