Global Timestamp Manager detects JS execution time

Source: Internet
Author: User

A handy gadget was written to test the loading speed of JavaScript scripts with different levels of confusion
1. [Code]timestamp.js

(function () {

if (typeof this. Timestamp = = ' undefined ') {
/**
* Global Timestamp Manager
* @author Janpoem
* Created at 2011.03.30
*/
This. Timestamp = (function () {

var pool = {
' Page_Init ': New Date ()
};

return {
Add:function (flag) {
Pool[flag] = new Date ();
},
Get:function (flag) {
Return typeof flag = = ' undefined '? Pool: (Pool[flag]? Pool[flag]: Pool);
},
Diff:function (To, diff) {
if (!diff)
diff = to, to = ' Page_Init ';
Return (Pool[to] && Pool[diff])? Pool[diff]-pool[to]: 0;
}
}
}) ();
}

}) ();
2. [Code] actual use case?
<!doctype html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>mootools Test page</title>http://www.huiyi8.com/donghua/?
<script type= "Text/javascript" >flash
(function () {

Because the actual speed at which a script file is loaded separately, the code for that object is loaded directly on the page
if (typeof this. Timestamp = = ' undefined ') {
/**
* Global Timestamp Manager
* @author Janpoem
* Created at 2011.03.30
*/
This. Timestamp = (function () {

var pool = {
' Page_Init ': New Date ()
};

return {
Add:function (flag) {
Pool[flag] = new Date ();
},
Get:function (flag) {
Return typeof flag = = ' undefined '? Pool: (Pool[flag]? Pool[flag]: Pool);
},
Diff:function (To, diff) {
if (!diff)
diff = to, to = ' Page_Init ';
Return (Pool[to] && Pool[diff])? Pool[diff]-pool[to]: 0;
}
}
}) ();
}

}) ();
</script>
<script type= "Text/javascript" src= "All.js" onload= "Timestamp.add (' load_complate ');" ></script>
<script type= "Text/javascript" >
Timestamp.add (' process_here#1 ');
</script>
<body>
<div id= "Mt_test" ></div>
<script type= "Text/javascript" >
Console.log (
Timestamp.diff (' load_complate '),//Download a script file speed
Timestamp.diff (' process_here#1 '),//HTML parse process here
Timestamp.diff (' load_complate ', ' process_here#1 ')
);
</script>
</body>

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.