


Methods to speed up IE's Javascript document output_javascript skills
Add the following code at the front of JavaScript
/*@ 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
// 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 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~
/*@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.
/*@cc_on
eval((function( props) {
var code = [];
for (var i = 0 l = props.length;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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
