Javascript page rendering speed test script sharing

Source: Internet
Author: User

Copy codeThe Code is as follows:/* Get the start timestamp of rendering and save it in the array PAGE_SPEED_TIME */
<Html> <script type = "text/javascript">/* tag */PAGE_SPEED_TIME = [new Date (). getTime ()]; </script> ......
</Head>

Copy codeThe Code is as follows:
......
/* Calculate the time consumed for page loading at the end of the page */
</Body>
<Script src = "http://ossweb-img.qq.com/images/js/pagespeed/page_speed_v2.js"> </script>


Next we will analyze the content of the page_speed_v2.js file. Reformat the script for easier reading.
Copy codeThe Code is as follows:
/* Execute the anonymous function build object PageSpeed */
; (Function (){
PageSpeed = {};/* There is no var keyword here, which is equivalent to referencing window. PageSpeed */

/**
* Bind the createScript Method
*
* @ Param String a script tag's src attribute value
* @ Param String B the id attribute value of the script tag
*/
PageSpeed. createScript = function (a, B ){
Var c = null;

If (document. getElementById (B) {/* the script tag already exists */
C = document. getElementById (B)
} Else {/* does not exist. Create a script tag */
C = document. createElement ('script ');
Var d = null;
If (document. getElementsByTagName ){
D = document. getElementsByTagName ('head') [0] | document.doc umentElement;
} Else {
D = document.doc umentElement;
}
D. insertBefore (c, d. firstChild);/* call the insertBefore method to insert the newly created script tag as the first child element */
}

/* The curly braces show the code snippet grouping function */
{
C. setAttribute ('type', 'text/html ');
C. setAttribute ('style', 'display: none ;');
C. setAttribute ('charset', 'gb2312 ');
C. setAttribute ('id', B );
C. setAttribute ('src', );
}

Return c;/* return the successfully created script Tag Element */
};

/**
* Bind the submitDataForPageSpeed Method
*
* @ Param Object a Map Object
*/
PageSpeed. submitDataForPageSpeed = function (){
Var B = {
'Name ':'',
'Rate': 0
};
B. name = a. name;
B. rate = a. rate;

/* The last element of a. PAGE_SPEED_TIME array minus the first element */
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]
}

/* Target script for submitting the speed test result */
Var c = 'HTTP: // pagespeed-ied.qq.com/r.cgi ';

/* Traverse the elements of array B and construct a query string */
Var d = [];
For (var p in B ){
D. push (p + '=' + B [p])
}
C + = '? '+ D. join ('&');

/* Create a script tag and submit the speed test result */
PageSpeed. createScript (c, 'submitforpagespeed ')
};

/**
* Bind the defaultSubmit Method
*
*/
PageSpeed. defaultSubmit = function (){
Var a = '';
Try {
A = location. host
} Catch (e ){}

Var B = {
'Name':,
'Rate': 1,
'Page _ SPEED_TIME ': []
};

If (! B. name ){
Return
}

If (! (Typeof (PAGE_SPEED_TIME) = 'object' & PAGE_SPEED_TIME instanceof Array )){
Return
}

/* Get the new timestamp */
PAGE_SPEED_TIME.push (new Date (). getTime ());

B. PAGE_SPEED_TIME = PAGE_SPEED_TIME;

/*
* I don't understand why I need a new variable aaa. Can I use a directly?
* The try... catch structure uses a new variable aaaa, but it is never 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 the submit method (for CDN speed measurement ?)
*
* @ Param String a name
*/
PageSpeed. submit = function (){
Var B = PageSpeed. cdn_page_speed_submitData;
Var c = {};
For (var p in B ){
C [p] = B [p]
}
C. name = a | c. name;
PageSpeed. submitDataForPageSpeed (c );
}
})();

Try {
/* Try to submit data in 1 second */
SetTimeout (function (){
PageSpeed. defaultSubmit ()
},
1000 );
} Catch (e ){}

/* For the last comment, the second part is a 32-bit hexadecimal number, which should be similar to the ETag used to mark the content version */
/* | XGv00 | ca82276cd78ac911d3d4310ba1408236 */

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.