-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecoindex.js
107 lines (94 loc) · 2.99 KB
/
ecoindex.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
var domQuantiles = [0, 47, 75, 159, 233, 298, 358, 417, 476, 537, 603, 674, 753, 843, 949, 1076, 1237, 1459, 1801, 2479, 594601];
var reqQuantiles = [0, 2, 15, 25, 34, 42, 49, 56, 63, 70, 78, 86, 95, 105, 117, 130, 147, 170, 205, 281, 3920];
var sizeQuantiles = [0, 1.37, 144.7, 319.53, 479.46, 631.97, 783.38, 937.91, 1098.62, 1265.47, 1448.32, 1648.27, 1876.08, 2142.06, 2465.37, 2866.31, 3401.59, 4155.73, 5400.08, 8037.54, 223212.26];
/**
* @param {array} quantiles
* @param {number} value
* @return {number}
*/
var calculateIndex = function (quantiles, value) {
for (var i = 1; i < quantiles.length; i++) {
if (value < quantiles[i]) {
return (i - 1 + (value - quantiles[i - 1]) / (quantiles[i] - quantiles[i - 1]));
}
}
return quantiles.length - 1;
};
/**
* @param {number} dom Number of DOM elements
* @param {number} req Number of HTTP requests
* @param {number} size Page size in ko
* @return {number}
*/
var getScore = function (dom, req, size) {
var domIndex = calculateIndex(domQuantiles, dom),
reqIndex = calculateIndex(reqQuantiles, req),
sizeIndex = calculateIndex(sizeQuantiles, size),
total = 100 - 5 * (3 * domIndex + 2 * reqIndex + sizeIndex) / 6;
return Math.round(total);
};
/**
* @param {number} score EcoIndex score
* @return {string}
*/
var getGrade = function (score) {
switch (true) {
case score >= 75:
return 'A';
case score >= 65:
return 'B';
case score >= 50:
return 'C';
case score >= 35:
return 'D';
case score >= 20:
return 'E';
case score >= 5:
return 'F';
default:
return 'G';
}
};
/**
* @param {number} score EcoIndex score
* @return {number}
*/
var getGreenhouseGasEmission = function (score) {
return Math.round(100 * (2 + 2 * (50 - score) / 100)) / 100;
};
/**
* @param {number} score EcoIndex score
* @return {number}
*/
var getWaterConsumption = function (score) {
return Math.round(100 * (3 + 3 * (50 - score) / 100)) / 100;
};
/**
* @param {number} dom Number of DOM elements
* @param {number} req Number of HTTP requests
* @param {number} size Page size in ko
* @return {number}
*/
module.exports.getEcoindex = function (dom, req, size) {
var score = getScore(dom, req, size);
return {
score: score,
grade: getGrade(score),
ghg: getGreenhouseGasEmission(score),
water: getWaterConsumption(score),
}
};
/**
* @deprecated
*/
module.exports.calculate = function (dom, req, size) {
console.debug('[Notice] The method "ecoindex.calculate()" is deprecated since version 1.3 and will be removed in version 2.0. Use "ecoindex.getEcoindex()" instead.');
return getScore(dom, req, size);
};
/**
* @deprecated
*/
module.exports.getNote = function (index) {
console.debug('[Notice] The method "ecoindex.getNote()" is deprecated since version 1.3 and will be removed in version 2.0. Use "ecoindex.getEcoindex()" instead.');
return getGrade(index);
};