egret 加载优化尝试- 多线程下载js代码 分布加载 (附测试代码)

egret 优化相关

网络加载优化

现有egret index 网络加载
旧版egret 加载js

优化思路:
采用 queue + object 的数据结构,保存需要加载的脚本,然后对脚本进行并行下载,在每个文件下载完成后进行检测,检测当前文件是否为array中的最前序,true时对下载完成的脚本进行加载
采用的方案就是并行下载+串行执行的方案进行加速(egret 编译完的js代码 存在依赖关系 加载的时候 需要按顺序加载)

附代码

<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>海豚思维</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <script>

        var myDate = new Date().getTime();
        console.log("开始时间:"+myDate)
    
    </script>
    <style>
        html, body {
           /*-ms-touch-action: none;*/
            background: #FFFFFF;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>

    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
         data-entry-class="Main"
         data-orientation="auto"
         data-scale-mode="showAll"
         data-frame-rate="30"
         data-multi-fingered="1"
         data-content-width="1562"
         data-content-height="1348"
         data-show-fps="false" data-show-log="false"
         data-show-fps-style="x:0,y:0,size:20,textColor:0xffffff,bgAlpha:0.9">
    </div>

<script>

    var timestamp = new Date().getTime()

    var startGame = function() {
        /**
         * {
         * "renderMode":, //Engine rendering mode, "canvas" or "webgl"
         * "audioType": 0 //Use the audio type, 0: default, 2: web audio, 3: audio
         * "antialias": //Whether the anti-aliasing is enabled in WebGL mode, true: on, false: off, defaults to false
         * "calculateCanvasScaleFactor": //a function return canvas scale factor
         * }
         **/

        console.log("总耗时:", timestamp - new Date().getTime())

        egret.runEgret({ renderMode: "canvas", audioType: 0, calculateCanvasScaleFactor:function(context) {
            var backingStore = context.backingStorePixelRatio ||
                context.webkitBackingStorePixelRatio ||
                context.mozBackingStorePixelRatio ||
                context.msBackingStorePixelRatio ||
                context.oBackingStorePixelRatio ||
                context.backingStorePixelRatio || 1;
            return (window.devicePixelRatio || 1) / backingStore;
        }});
    }

    //脚本队列
    var queueScripts = [];
    var loadIndex = 0;

    var loadScriptXhrInjection = function(url){
        var qL = queueScripts.length;
        var qScript = {key:url, response: null, done: false}; 
        queueScripts[qL] = qScript;
        var xhrObj = new XMLHttpRequest();
        xhrObj.onreadystatechange = function() {
            if(xhrObj.readyState == 4 && xhrObj.status == 200){
                //有顺序要求的脚本需要添加的队列,按添加顺序执行
                queueScripts[qL].response = xhrObj.responseText;

                var enddate = new Date().getTime();
                var gap = enddate - myDate;
                console.log(url + "下载完成:" + gap)

                //执行脚本队列
                injectScripts();
            }
        }
        console.log("开始下载url:" + url)
        xhrObj.open("GET", url, true);
        xhrObj.send(null);
    }

    var injectScripts =  function(){
        var len = queueScripts.length;
        //按顺序执行队列中的脚本
        for (var i = 0; i < len; i++) {
            var qScript = queueScripts[i];
            //没有执行
            if(!qScript.done){
                //没有加载完成
                if(!qScript.response){
                    //停止,等待加载完成, 由于脚本是按顺序添加到队列的,因此这里保证了脚本的执行顺序
                    break;
                }else{//已经加载完成了
                    var script = document.createElement("script");
                    document.getElementsByTagName("head")[0].appendChild(script);
                    script.text = qScript.response;
                    qScript.done = true;
                    loadIndex++;
                    console.log("加载url:"+qScript.key)
                }
            }
        };

        if(loadIndex === len){
            startGame();
        }
    };

    var xhr = new XMLHttpRequest();
    xhr.open('GET', './manifest.json?v=' + Math.random(), true);
    xhr.addEventListener("load", function () {
        var manifest = JSON.parse(xhr.response);
        var list = manifest.initial.concat(manifest.game);
        list.forEach(function(element) {
            loadScriptXhrInjection(element);
        }, this);
    });
    xhr.send(null);

</script>

</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值