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

May 16, 2016 pm 06:14 PM
javascript

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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 implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

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 forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

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 and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

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

See all articles