forked from jslayer/css-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_script.js
98 lines (76 loc) · 2.62 KB
/
content_script.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
/**
* @type {Performance}
*/
performance;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
switch (request) {
case 'css-perf':
calculate();
break;
}
sendResponse(true);
});
function calculate(){
var raw_result, result, i0, l0, i1, l1, i2, l2, ru, t, s, ss, els, el, _F, href;
raw_result = {};
result = {};
for (i0 = 0, l0 = document.styleSheets.length; i0 < l0; i0++) {
ss = document.styleSheets[i0].cssRules;
href = document.styleSheets[i0].href;
if (!ss) {
//todo - i.e. github doesn't allow to read the document.styleSheets[XX].cssRules; Same origin ?
continue;
}
if (href) {
href = href.match(/[^\/]+$/)[0];
}
for (i1 = 0, l1 = ss.length; i1 < l1; i1++) {
ru = ss[i1];
t = 0;
if (!ru.selectorText || ru.selectorText[0] === '@') {
//todo - skip selectors started from @ char: ie @font-face { font-family: Helvetica; }
continue;
}
s = performance.now();
els = document.querySelectorAll(ru.selectorText);
s = performance.now() - s;
for (i2 = 0, l2 = els.length; i2 < l2; i2++) {
el = els[i2];
el.style.visibility = "hidden";
el.style.visibility = "";
t -= performance.now();
_F = el.offsetWidth;
t += performance.now();
}
if (typeof raw_result[ru.selectorText] === 'undefined') {
raw_result[ru.selectorText] = [];
}
raw_result[ru.selectorText].push({
total : t,
select : s,
length : l2,
href : href
});
}
}
for (i0 in raw_result) {
if (raw_result.hasOwnProperty(i0)) {
els = raw_result[i0];
l0 = els.length;
els.forEach(function(el, i1){
var ru = i0;
if (l0 > 1) {
ru += ' (' + i1 + ')';
}
result[ru] = {
'querySelectorAll (ms)' : parseFloat(el.select.toFixed(3)),
'Source' : el.href,
'R-n average (ms)' : el.length > 0 ? parseFloat((el.total / el.length).toFixed(3)) : 0,
'R-n (ms)' : parseFloat(el.total.toFixed(3)),
'Matches' : el.length
};
});
}
}
console.table(result);
}