-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathperformance.js
More file actions
186 lines (163 loc) · 6.17 KB
/
performance.js
File metadata and controls
186 lines (163 loc) · 6.17 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Performance monitoring and optimization utilities
(function() {
'use strict';
// Performance metrics collection
const metrics = {
loadTime: 0,
domContentLoaded: 0,
firstPaint: 0,
firstContentfulPaint: 0,
largestContentfulPaint: 0,
cumulativeLayoutShift: 0,
firstInputDelay: 0
};
// Collect Core Web Vitals and other performance metrics
function collectMetrics() {
// Navigation timing
if (performance.timing) {
metrics.loadTime = performance.timing.loadEventEnd - performance.timing.navigationStart;
metrics.domContentLoaded = performance.timing.domContentLoadedEventEnd - performance.timing.navigationStart;
}
// Paint timing
if (performance.getEntriesByType) {
const paintEntries = performance.getEntriesByType('paint');
paintEntries.forEach(entry => {
if (entry.name === 'first-paint') {
metrics.firstPaint = entry.startTime;
} else if (entry.name === 'first-contentful-paint') {
metrics.firstContentfulPaint = entry.startTime;
}
});
}
// Largest Contentful Paint
if (performance.getEntriesByType) {
const lcpEntries = performance.getEntriesByType('largest-contentful-paint');
if (lcpEntries.length > 0) {
metrics.largestContentfulPaint = lcpEntries[lcpEntries.length - 1].startTime;
}
}
// Cumulative Layout Shift
if (performance.getEntriesByType) {
const clsEntries = performance.getEntriesByType('layout-shift');
metrics.cumulativeLayoutShift = clsEntries.reduce((sum, entry) => {
return sum + (entry.hadRecentInput ? 0 : entry.value);
}, 0);
}
// First Input Delay
if (performance.getEntriesByType) {
const fidEntries = performance.getEntriesByType('first-input');
if (fidEntries.length > 0) {
metrics.firstInputDelay = fidEntries[0].processingStart - fidEntries[0].startTime;
}
}
}
// Memory usage monitoring
function getMemoryUsage() {
if (performance.memory) {
return {
used: Math.round(performance.memory.usedJSHeapSize / 1048576 * 100) / 100,
total: Math.round(performance.memory.totalJSHeapSize / 1048576 * 100) / 100,
limit: Math.round(performance.memory.jsHeapSizeLimit / 1048576 * 100) / 100
};
}
return null;
}
// Resource timing analysis
function analyzeResources() {
if (!performance.getEntriesByType) return null;
const resources = performance.getEntriesByType('resource');
const analysis = {
total: resources.length,
totalSize: 0,
slowResources: [],
cacheHits: 0,
cacheMisses: 0
};
resources.forEach(resource => {
analysis.totalSize += resource.transferSize || 0;
if (resource.duration > 1000) { // Resources taking more than 1 second
analysis.slowResources.push({
name: resource.name,
duration: Math.round(resource.duration),
size: resource.transferSize || 0
});
}
// Check cache efficiency
if (resource.transferSize === 0 && resource.decodedBodySize > 0) {
analysis.cacheHits++;
} else if (resource.transferSize > 0) {
analysis.cacheMisses++;
}
});
return analysis;
}
// Performance score calculation
function calculateScore() {
let score = 100;
// Deduct points for poor metrics
if (metrics.firstContentfulPaint > 1800) score -= 20;
if (metrics.largestContentfulPaint > 2500) score -= 20;
if (metrics.cumulativeLayoutShift > 0.1) score -= 20;
if (metrics.firstInputDelay > 100) score -= 20;
if (metrics.loadTime > 3000) score -= 20;
return Math.max(0, score);
}
// Generate performance report
function generateReport() {
collectMetrics();
const memory = getMemoryUsage();
const resources = analyzeResources();
const score = calculateScore();
return {
score,
metrics,
memory,
resources,
timestamp: new Date().toISOString()
};
}
// Log performance report to console (development only)
function logReport() {
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
const report = generateReport();
console.group('🚀 Performance Report');
console.log(`Overall Score: ${report.score}/100`);
console.log('Core Web Vitals:', report.metrics);
if (report.memory) console.log('Memory Usage:', report.memory);
if (report.resources) console.log('Resource Analysis:', report.resources);
console.groupEnd();
}
}
// Initialize performance monitoring
function init() {
// Wait for page to be fully loaded
if (document.readyState === 'complete') {
setTimeout(logReport, 1000);
} else {
window.addEventListener('load', () => {
setTimeout(logReport, 1000);
});
}
// Monitor for performance issues
let lastMemoryCheck = Date.now();
setInterval(() => {
const memory = getMemoryUsage();
if (memory && memory.used > 100) { // More than 100MB
console.warn('High memory usage detected:', memory);
}
}, 30000); // Check every 30 seconds
}
// Expose utilities globally for debugging
window.PerformanceMonitor = {
generateReport,
getMemoryUsage,
analyzeResources,
metrics
};
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();