-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprofiling.js
71 lines (60 loc) · 1.55 KB
/
profiling.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
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
Object.prototype.foldl = function(f, a){
for (var i in this) {
if (this.hasOwnProperty(i)) {
a = f(a, i, this[i]);
}
};
return a;
}
Object.prototype.map = function(f) {
return this.foldl([], function(a, i, e){
a.push(f(i, e));
return a;
});
}
Array.prototype.sum = function() {
this.foldl(0, function(a, i, e){return a + e;});
}
Array.prototype.avg = function() {
this.sum() / this.length;
}
function Profiling(keyFunc, pauseOn){
this.keyFunc = keyfunc;
this.pauseOn = pauseOn;
this.samples = {};
this.sampleCount = 0;
this.profile = function(obj, func){
if(typeof samples[keyFunc(obj)] == 'undefined'){
samples[keyFunc(obj)] = [];
}
var start = window.performance.now()
var result = func();
samples[keyFunc(obj)].push(window.performance.now() - start);
return result;
}
this.breakdown = function(){
return samples.inject({}, function(a, i, e){
a[i] = {sum: e.sum, avg: e.avg, max: e.max, min: e.min};
return a;
});
}
}
if(typeof window.profile_count == 'undefined'){
window.profile_count = 0;
}
if(typeof window.profiles == 'undefined'){
window.profiles = {};
}
if(!window.profiles[form.form.ctor]){
window.profiles[form.form.ctor] = [];
}
window.profiles[form.form.ctor].push(Date.now() - start);
if(window.profile_count++ >= 10000){
debugger;
}