-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
123 lines (115 loc) · 3.42 KB
/
helpers.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
(function() {
'use strict';
var perfTest = {
entries: {},
/**
* Start capturing time
* @param {string} name
*/
start: function(name) {
if (!this.entries[name]) {
this.entries[name] = {
timings: [],
min: Infinity,
max: -Infinity,
sum: 0,
tmp: null
};
}
this.entries[name].tmp = performance.now();
},
/**
* Stop capturing time
* @param {string} name
*/
end: function(name) {
if (this.entries[name]) {
var value = performance.now() - this.entries[name].tmp;
this.entries[name].timings.push(value);
if (this.entries[name].min > value) {
this.entries[name].min = value;
}
if (this.entries[name].max < value) {
this.entries[name].max = value;
}
this.entries[name].sum += value;
}
},
/**
* Print the results
*/
results: function() {
Object.keys(this.entries).forEach(function(name) {
if (!this.entries[name].timings.length) {
return;
}
var mean = this.entries[name].timings.reduce(function(result, timing) {
return result + timing;
}) / this.entries[name].timings.length;
var median = this.entries[name].timings.sort(function(a, b) {
return a - b;
})[Math.floor(this.entries[name].timings.length / 2)];
console.log(`--${name}--`);
console.log(
`mean: ${mean},
median: ${median},
sum: ${this.entries[name].sum},
max: ${this.entries[name].max},
min: ${this.entries[name].min},
count: ${this.entries[name].timings.length}`);
}, this);
}
};
var normalization = {
/**
* Create value instance
* @param {string} key
*/
reset: function(key) {
this.items = this.items || {};
this.items[key] = {
min: Infinity,
max: -Infinity,
divider: null
}
},
/**
* Add new value for comparison
* @param {string} key
* @param {number} value
*/
push: function(key, value) {
var item = this.items[key];
if (item.min > value) {
item.min = value;
}
if (item.max < value) {
item.max = value;
}
},
/**
* When done collecting data, calculate the divider
* @param {string} key
*/
done: function(key) {
this.items[key].divider = this.items[key].max - this.items[key].min;
},
/**
* Returns the normalized value
* @param {string} key
* @param {number} value
* @returns {number}
*/
normalize: function(key, value) {
var item = this.items[key];
if (item.divider) {
return (value - item.min) / item.divider;
}
return 0;
}
};
window.helpers = {
perfTest: perfTest,
normalization: normalization
}
})();