Home > Web Front-end > JS Tutorial > body text

JavaScript page rendering speed test script sharing_javascript skills

WBOY
Release: 2016-05-16 16:52:21
Original
1298 people have browsed it

Copy code The code is as follows:
/* Get the timestamp of the start of rendering and save it in the array PAGE_SPEED_TIME* /

......

Copy code The code is as follows:

......
/* Page At the end, calculate the time it takes to load the page*/



Let’s analyze the contents of the page_speed_v2.js file. Reformat this script to make it easier to read.

Copy code The code is as follows:

/* 执行匿名函数构建对象PageSpeed */
;(function() {
    PageSpeed = {};/* 这里没有 var 关键字, 相当于引用的是 window.PageSpeed */

 /**
     * 绑定createScript方法
  *
  * @param String a script标签的src属性的值
  * @param String b script标签的id属性的值
  */
    PageSpeed.createScript = function(a, b) {
        var c = null;

        if (document.getElementById(b)) { /* script标签已经存在 */
            c = document.getElementById(b)
        } else { /* 不存在, 创建script标签 */
            c = document.createElement('script');
            var d = null;
            if (document.getElementsByTagName) {
                d = document.getElementsByTagName('head')[0] || document.documentElement;
            } else {
                d = document.documentElement;
            }
            d.insertBefore(c, d.firstChild); /* 调用insertBefore方法, 将新创建的script标签插入为第一个子元素 */
        }

  /* 这里的花括号起到代码片段分组功能 */
  {
            c.setAttribute('type', 'text/html');
            c.setAttribute('style', 'display:none;');
            c.setAttribute('charset', 'gb2312');
            c.setAttribute('id', b);
            c.setAttribute('src', a);
        }

        return c; /* 返回创建成功的script标签元素 */
    };

 /**
     * 绑定 submitDataForPageSpeed 方法
  *
  * @param Object a Map对象
  */
    PageSpeed.submitDataForPageSpeed = function(a) {
        var b = {
            'name': '',
            'rate': 0
        };
        b.name = a.name;
        b.rate = a.rate;

  /* a.PAGE_SPEED_TIME数组的最后一个元素减去第一个元素 */
        b['1'] = a.PAGE_SPEED_TIME[a.PAGE_SPEED_TIME.length - 1] - a.PAGE_SPEED_TIME[0];

        for (var i = 1; i < a.PAGE_SPEED_TIME.length - 1; i++) {
            b[(i + 1) + ''] = a.PAGE_SPEED_TIME[i] - a.PAGE_SPEED_TIME[0]
        }

  /* 提交速度测试结果的目的脚本 */
        var c = 'http://pagespeed-ied.qq.com/r.cgi';

  /* 遍历 b 数组的元素, 构建 query string */
        var d = [];
        for (var p in b) {
            d.push(p + '=' + b[p])
        }
        c += '?' + d.join('&');

  /* 创建script标签提交测速结果 */
        PageSpeed.createScript(c, 'submitForPageSpeed')
    };

 /**
  * 绑定 defaultSubmit 方法
  *
  */
    PageSpeed.defaultSubmit = function() {
        var a = '';
        try {
            a = location.host
        } catch(e) {}

var b = {
'name': a,
'rate': 1,
'PAGE_SPEED_TIME': []
};

if (!b.name) {
return
}

        if (! (typeof(PAGE_SPEED_TIME) == 'object' && PAGE_SPEED_TIME instanceof Array)) {
                                                                                                                                                           
/* Get new timestamp */

PAGE_SPEED_TIME.push(new Date().getTime());


b.PAGE_SPEED_TIME = PAGE_SPEED_TIME;

/*

* I don’t understand why a new variable aaa is needed here. Isn’t it possible to use a directly?

* The try ... catch structure uses a new variable aaaa but It has never been used later, why? Should it be aaa?
*/
var aaa = '';
try {
aaaa = location.host
} catch(e) {}

var c = Math.floor(Math.random() * 10000);

If (aaa == "ktv.qq.com" || aaa == "ttd.qq.com" || aaa == "tian.qq.com" || aaa == "sura.qq.com " || aaa == "gw.tnt.qq.com" || aaa == "007.qq.com") {

                    c = Math.floor(Math.random() * 1000);

}

/* Submit data with random probability */

If (c <= b.rate * 1) {

PageSpeed.submitDataForPageSpeed(b);
}
};

/**

* Bind submit method (used for CDN speed test?)

*
* @param String a Name
*/
PageSpeed.submit = function(a) {
var b = PageSpeed.cdn_page_speed_submitData;
var c = {};
for (var p in b) {
                c[p] =                                                                                     })();

try {
/* Try to submit data after 1 second*/
setTimeout(function() {
PageSpeed.defaultSubmit()
},
1000);

} catch(e) {}


/* The last comment, the second paragraph is a 32-digit hexadecimal number, which should be similar to ETag used to mark the content version */
/* |xGv00|ca82276cd78ac911d3d4310ba1408236 */


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template