-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.profile.js
168 lines (134 loc) · 4.86 KB
/
jquery.profile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(function(){
var log = [];
var handle = jQuery.event.handle, ready = jQuery.ready;
var internal = false;
var eventStack = [];
var curEvent = log[0] = { event: "inline", methods: [], duration: 0 };
if ( !jQuery.readyList )
jQuery.readyList = [];
jQuery.readyList.unshift(function(){
if ( curEvent && curEvent.event == "inline" && curEvent.methods.length == 0 ) {
log.shift();
}
if ( curEvent )
eventStack.push( curEvent );
var e = curEvent = log[log.length] = { event: "ready", target: formatElem(document), methods: [] };
var start = (new Date).getTime();
jQuery(document).bind("ready", function(){
e.duration = (new Date).getTime() - start;
curEvent = eventStack.pop();
});
});
jQuery.event.handle = function(event){
var pos = log.length;
if ( curEvent )
eventStack.push( curEvent );
var e = curEvent = log[pos] = { event: event.type, target: formatElem(event.target), methods: [] };
var start = (new Date).getTime();
var ret = handle.apply( this, arguments );
e.duration = (new Date).getTime() - start;
curEvent = eventStack.pop();
if ( e.methods.length == 0 && e.duration <= 1 ) {
log.splice( pos, 1 );
}
return ret;
};
for ( var method in jQuery.fn ) (function(method){
if ( method == "init" ) return;
var old = jQuery.fn[method];
jQuery.fn[method] = function(){
if ( !internal && curEvent ) {
internal = true;
var m = curEvent.methods[curEvent.methods.length] = { name: method, inLength: this.length, args: formatArgs(arguments) };
var start = (new Date).getTime();
var ret = old.apply( this, arguments );
m.duration = (new Date).getTime() - start;
if ( curEvent.event == "inline" )
curEvent.duration += m.duration;
if ( ret && ret.jquery )
m.outLength = ret.length;
internal = false;
return ret;
} else {
return old.apply( this, arguments );
}
};
})(method);
var init = jQuery.fn.init;
jQuery.fn.init = function(){
var args = Array.prototype.slice.call( arguments );
if ( typeof args[1] == "undefined" )
args.pop();
if ( !internal && curEvent ) {
internal = true;
var m = curEvent.methods[curEvent.methods.length] = { name: "jQuery", args: formatArgs(args) };
var start = (new Date).getTime();
var ret = init.apply( this, arguments );
m.duration = (new Date).getTime() - start;
if ( curEvent.event == "inline" )
curEvent.duration += m.duration;
m.outLength = ret.length;
internal = false;
return ret;
} else {
return init.apply( this, args );
}
};
jQuery.fn.init.prototype = init.prototype;
jQuery.getProfile = function(){
return log;
};
jQuery.displayProfile = function(){
var str = "<div style='text-align:left;background:#FFF;color:#000;font-size:10px;width:400px;height:400px;overflow:auto;padding:8px;margin:10px;'>";
for ( var i = 0; i < log.length; i++ ) {
var total = log[i].duration;
str += "<big><b>Event: " + log[i].event + " (" + log[i].duration + "ms)</b></big><br/>" +
"<table><tr><th>%</th><th>(ms)</th><th>Method</th><th>in</th><th>out</th></tr>";
var methods = log[i].methods;
for ( var m = 0; m < methods.length; m++ ) {
var method = methods[m];
str += "<tr><td>" + ((method.duration / total) * 100).toFixed(1) + "%</td><td>" +
method.duration + "</td><td>" + (method.name == "jQuery" ? "" : " .") + method.name + "(" + method.args + ")</td>" +
"<td>" + (method.inLength || "") + "</td><td>" + (method.outLength || "") + "</td></tr>";
}
str += "</table>";
}
jQuery("body").append( str + "</div>" );
};
function formatElem(elem) {
var str = "";
if ( elem.tagName ) {
str = "<" + elem.tagName.toLowerCase();
if ( elem.id )
str += "#" + elem.id;
if ( elem.className )
str += "." + elem.className.replace(/ /g, ".");
str += ">";
} else {
str = elem.nodeName;
}
return str;
}
function formatArgs(args) {
var str = [];
for ( var i = 0; i < args.length; i++ ) {
var item = args[i];
if ( item && item.constructor == Array ) {
str.push( "Array(" + item.length + ")" );
} else if ( item && item.jquery ) {
str.push( "jQuery(" + item.length + ")" );
} else if ( item && item.nodeName ) {
str.push( formatElem( item ) );
} else if ( item && typeof item == "function" ) {
str.push( "function()" );
} else if ( item && typeof item == "object" ) {
str.push( "{...}" );
} else if ( typeof item == "string" ) {
str.push( '"' + item.replace(/&/g, "&").replace(/</g, "<") + '"' );
} else {
str.push( item + "" );
}
}
return str.join(", ");
}
})();