Home >Web Front-end >JS Tutorial >Methods to speed up IE's Javascript document output_javascript skills

Methods to speed up IE's Javascript document output_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:14:481000browse

Add the following code at the front of JavaScript

Copy the code The code is as follows:

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

By adding such a line of code, IE’s document access speed can be increased by at least 5 times
The following is before adding Compare the code with the added test
Copy the code The code is as follows:

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

Copy code 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 of all, if the document is called directly in IE, the internal function of the window object will be executed, and this is relatively inefficient. According to this, the following processing can improve the speed:
var doc = document;

document; // Slower
doc; // This is faster than the above (document)

Although it can be used directly as written above, it is a bit troublesome to replace the document where it was used before. So, look at the following:
var doc = document;
var document = doc;
It would be great if it could be implemented...

People who know JavaScript should know that JavaScript The variable is generated at the beginning, so the document here becomes undefined.
It doesn’t matter, keep improving~
var doc = document;
eval('var document = doc');

The function of eval is to change the variable within the scope. In this case, The subsequent document can be used normally.
Finally, add the condition that is only valid in IE, just like the following~
Copy code Code As follows:

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

Draw inferences from one example and write it like the following. Global variables other than the document can also use the above method to speed up the process.
Copy code The code is as follows:

/*@cc_on
eval((function( props) {
var code = [];
for (var i = 0 l = props.length;ivar 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 of all, if the document is called directly in IE, the internal function of the window object will be executed, and this is relatively inefficient. According to this point, the following processing can improve the speed:

This statement is wrong..

The reason why there are differences between your before and after tests is that the main difference lies in the scope chain search.
Your The code is in the global execution environment. Therefore, under IE, it will access the global object to find the member with the key 'document'. In IE, this object is a host object implemented by COM. It is not in global. If it is not in global, then Search again in the window, causing the speed to slow down.

The same global object Math will not cause this problem. The reason is that Math is on Global. It is found with a scope chain search.

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

And if you only use ie conditional comments once in the global scope, first of all, non-ie users will not be able to enjoy the benefits of the shortened scope. Of course, non-ie users will not have one more chain of responsibility search for global->window.

The core optimization here is to shorten the scope chain. Although newer versions such as Opera Chrome Safari have optimized scope chain search, we believe that shortening the scope chain will have a positive effect on old browsers. .And it will not have too negative impact on optimized browsers.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:jquery json example code_jqueryNext article:jquery json example code_jquery

Related articles

See more