forked from benthemonkey/source-map
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstats.js
36 lines (29 loc) · 816 Bytes
/
stats.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
// Original author: Jim Blandy
function Stats(unit) {
this.unit = unit || "";
this.x0 = this.x1 = this.x2 = 0;
}
Stats.prototype.take = function (x) {
this.x0 += 1;
this.x1 += x;
this.x2 += x*x;
}
Stats.prototype.samples = function () {
return this.x0;
};
Stats.prototype.total = function () {
return this.x1;
};
Stats.prototype.mean = function () {
return this.x1 / this.x0;
};
Stats.prototype.stddev = function () {
return Math.sqrt(this.x0 * this.x2 - this.x1 * this.x1) / (this.x0 - 1);
};
Stats.prototype.toString = function () {
return "[Stats " +
"samples: " + this.samples() + ", " +
"total: " + this.total() + " " + this.unit + ", " +
"mean: " + this.mean() + " " + this.unit + ", " +
"stddev: " + this.stddev() + " " + this.unit + "]";
};