How to accelerate JavaScript of IE

Source: Internet
Author: User

Set the followingCodeAdd to the beginning of JavaScript
/* @ Cc_on _ d = Document; EVAL ('var document = _ D ')@*/

The document with such a line of code Ie can be accessed at least five times faster
The following is the test comparison code before and after joining
// Before
VaR date = new date;
For (VAR I = 0; I <100000; I ++) document;
Alert (new date-date); // 643

/* @ 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

The speed has improved a lot!

Explanation:
First, if the document in IE is directly called, the internal function of the window object is executed, which is inefficient. Based on this, the following processing can increase the speed:
VaR Doc = document;

Document; // slow
Doc; // This is faster than the above (document)

Although it can be directly used for writing like above, it is a little troublesome to replace all the documents used previously. So, let's look at the following:
VaR Doc = document;
VaR document = Doc;
That would be great if it could be implemented ......

People who know JavaScript should know that JavaScript variables are generated at the very beginning, so the document here becomes undefined.
It doesn't matter. continue to improve ~
VaR Doc = document;
Eval ('var document = doc ');

The eval function is to change the variable within the scope. In this way, the subsequent document can be used normally.
Finally, add the conditions that are only valid in IE, just like the following ~
/* @ Cc_on
VaR Doc = document;
Eval ('var document = doc ');
@*/

In the following way, global variables other than document can also be used to accelerate the process.
/* @ 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 ('')));
@*/

Reprinted:Http://purpen.javaeye.com /? Page = 8 & show_full = true

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.