JavaScript function inert Loading

Source: Internet
Author: User

I recently read about the Javascript advanced programming, and I have a lot to learn about. I will take some reading notes in the next few days. I wrote an article about Ajax preliminary understanding. There is a function in it to create the XMLHTTPRequest object. The reason for browser compatibility is that the code written is judged by a large number of if statements or try statements, the catch statement directs the function to the correct code.

<script type="text/javascript">            function createXHR(){                var xhr = null;                try {                    // Firefox, Opera 8.0+, Safari,IE7+                    xhr = new XMLHttpRequest();                }                catch (e) {                    // Internet Explorer                     try {                        xhr = new ActiveXObject("Msxml2.XMLHTTP");                    }                    catch (e) {                        try {                            xhr = new ActiveXObject("Microsoft.XMLHTTP");                        }                        catch (e) {                            xhr = null;                        }                    }                }                return xhr;            }        </script>

Each time you call this function, you must first check whether the browser supports the built-in xmlhyyprequest object. If not, check whether the browser supports ActiveX-based XMLHttpRequest, this is true for every call to this function. In fact, after the first execution, if the browser supports a specific XMLHTTPRequest object, this support will not change during the next execution, there is no need to perform a side check. Even if there is only one If statement, the execution is certainly slower than it is. If we can make the if statement not executed every time, therefore, the execution speed can be improved with frequent calls. The solution is a kind of technique called inertia loading.

Inert Loading

Inert loading indicates that the branch of the function execution will only be executed when the function is used for the first time. During the first call, the function will be overwritten as another function to be executed in an appropriate way, in this way, any call to the original function will no longer need to go through the Execution Branch. The createxhr function can be rewritten as follows:

function createXHR(){                var xhr=null;                if(typeof XMLHttpRequest !='undefined'){                    xhr = new XMLHttpRequest();                    createXHR=function(){                        return new XMLHttpRequest();                    }                }else{                    try {                        xhr = new ActiveXObject("Msxml2.XMLHTTP");                        createXHR=function(){                            return new ActiveXObject("Msxml2.XMLHTTP");                        }                    }                    catch (e) {                        try {                            xhr = new ActiveXObject("Microsoft.XMLHTTP");                            createXHR=function(){                                return new ActiveXObject("Microsoft.XMLHTTP");                            }                        }                        catch (e) {                            createXHR=function(){                                return null;                            }                        }                    }                }                return xhr;            }

During the first execution of createxhr in this inert load, each branch will assign a new value to createxhr, overwrite the original function, and return the xhr object, during the second execution, the overwritten function will be called directly, so that each branch does not have to be executed for re-detection.

Advantages

There are two main advantages of the lazy loading function. The first is the obvious efficiency problem. Although the function will mean that the value assignment is slower during the first execution, however, subsequent calls will be faster because of Avoiding repeated detection. The second is that the appropriate code to be executed is only executed when the function is actually called, many JavaScript libraries execute many branches based on different browsers during loading, and set all the implementations. The execution time of the initial script is not affected by the calculation delay of the inert loading function.

Reprinted to http://www.cnblogs.com/dolphinX/p/3251165.html

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.