美文网首页
loadScript性能测试

loadScript性能测试

作者: itstrive | 来源:发表于2017-06-02 09:26 被阅读0次
    function loadJS(url, callback) {
                var head = window.document.getElementsByTagName("head")[0];
                var script = window.document.createElement('script');
                script.onload = script.onreadystatechange = script.onerror = function() {
                    if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
                    script.onload = script.onreadystatechange = script.onerror = null;
                    script.src = '';
                    script.parentNode.removeChild(script);
                    script = null;
                    if (callback) {
                        callback();
                    }
                };
                script.charset = "utf-8";
                script.async = true;
                script.src = url;
                try {
                    head.appendChild(script);
                } catch (exp) {}
            }
    
            function loadJS2(url,callback){
                var head=document.getElementsByTagName('head')[0];
                var script=document.createElement('script');
    
                if(script.readyState){
                    script.onreadystatechange=function(){
                        if(script.readyState=='loaded' || script.readyState=='complete'){
                            script.parentNode.removeChild(script);
    
                            callback && callback();
                        }
                    };
                }else{
                    script.onload=function(){
                        script.parentNode.removeChild(script);
    
                        callback && callback();
                    };
                }
    
                script.src=url;
                head.appendChild(script);
            }
    
            console.time('start');
            for(var i=0; i<100; i++){
                loadJS2('https://code.jquery.com/jquery-3.2.1.js',function(){
                    console.log('count:'+i);
                });
            }
            console.timeEnd('start');
    

    相关文章

      网友评论

          本文标题:loadScript性能测试

          本文链接:https://www.haomeiwen.com/subject/jbiyfxtx.html