How to accelerate IE's Javascriptdocument output _ javascript skills

Source: Internet
Author: User
The access speed of the document with such a line of code IE can be increased by at least five times. It is definitely a good method for friends who use document on pages. Add the following code to the beginning of JavaScript

The Code is as follows:


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

The Code is as follows:


// Before
Var date = new Date;
For (var I = 0; I <100000; I ++) document;
Alert (new Date-date); // 643


The Code is 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


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 ~

The Code is as follows:


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

The Code is as follows:


/* @ Cc_on
Eval (function (props ){
Var code = [];
For (var I = 0 l = props. length; 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 ('')));
@*/


The following is Franky's reply:
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:

This is not true ..

The main difference between your tests is the scope chain search.
Your code is in the global execution environment. therefore, in IE, The global object will be accessed to find the Member whose key is 'document. in ie, this object is a Host Object implemented by com +. he is not in global. if not, search again in window. this slows down the speed.

Math, the same Global object, won't cause this problem. The reason is that Math is found at Global.

For optimization, one suggestion is:
Var win = window, doc = document, undefined;
Within the scope of each layer, it makes sense if this member is used more than twice.

If you only use the ie condition annotation once in the global scope, you will not be able to enjoy the benefits of shortening the scope without ie. of course, non-ie does not have one more global-> window responsibility chain lookup.

The core of optimization here is to shorten the scope chain. although the latest versions such as opera chrome javasai have optimized the scope chain search. but we think it is to shorten the scope chain. it has a positive effect on old browsers. in addition, it will not bring too negative effects to browsers with optimized features.
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.