-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs-performance-manager.js
More file actions
147 lines (125 loc) · 4.09 KB
/
js-performance-manager.js
File metadata and controls
147 lines (125 loc) · 4.09 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
export class JSPerformanceManager {
constructor() {
this.taskQueue = [];
this.frameScheduler = new FrameScheduler();
this.executionProfiler = new ExecutionProfiler();
}
// Break large tasks into smaller chunks
breakIntoMicroTasks(task, chunkSize = 5) {
const chunks = [];
for (let i = 0; i < task.length; i += chunkSize) {
chunks.push(task.slice(i, i + chunkSize));
}
return chunks;
}
// Execute tasks across multiple frames
async executeTasksOverFrames(tasks) {
for (const task of tasks) {
await this.frameScheduler.scheduleTask(() => {
task();
});
}
}
// Optimize object creation to reduce GC pressure
createOptimizedBuildingSpawner() {
return {
// Pre-allocate arrays to avoid runtime allocation
tempPosition: new THREE.Vector3(),
tempRotation: new THREE.Euler(),
tempScale: new THREE.Vector3(),
// Reuse objects instead of creating new ones
spawnBuilding(template, position, rotation, scale) {
this.tempPosition.copy(position);
this.tempRotation.copy(rotation);
this.tempScale.copy(scale);
const building = template.clone();
building.position.copy(this.tempPosition);
building.rotation.copy(this.tempRotation);
building.scale.copy(this.tempScale);
return building;
}
};
}
// Reduce function call overhead
optimizeCriticalPaths() {
// Cache frequently accessed properties
const cache = {
mathPi: Math.PI,
mathPi2: Math.PI * 2,
performance: performance,
now: performance.now.bind(performance)
};
return cache;
}
}
class FrameScheduler {
constructor() {
this.tasks = [];
this.running = false;
this.frameCount = 0;
this.maxTasksPerFrame = 3; // Limit tasks per frame
}
scheduleTask(task) {
return new Promise((resolve) => {
this.tasks.push({ task, resolve });
if (!this.running) {
this.startProcessing();
}
});
}
startProcessing() {
this.running = true;
this.processFrame();
}
processFrame() {
const startTime = performance.now();
const frameTimeLimit = 12; // 12ms to stay under 16.67ms budget
let tasksProcessed = 0;
while (this.tasks.length > 0 &&
performance.now() - startTime < frameTimeLimit &&
tasksProcessed < this.maxTasksPerFrame) {
const { task, resolve } = this.tasks.shift();
task();
resolve();
tasksProcessed++;
}
if (this.tasks.length > 0) {
requestAnimationFrame(() => this.processFrame());
} else {
this.running = false;
}
}
}
class ExecutionProfiler {
constructor() {
this.profiles = new Map();
}
profile(name, fn) {
const start = performance.now();
const result = fn();
const duration = performance.now() - start;
if (!this.profiles.has(name)) {
this.profiles.set(name, { total: 0, count: 0, avg: 0 });
}
const profile = this.profiles.get(name);
profile.total += duration;
profile.count++;
profile.avg = profile.total / profile.count;
// Warn about slow functions
if (duration > 5) {
// Slow function detected - consider optimization
}
return result;
}
getReport() {
const report = {};
for (const [name, profile] of this.profiles) {
report[name] = {
averageTime: profile.avg.toFixed(2) + 'ms',
totalTime: profile.total.toFixed(2) + 'ms',
callCount: profile.count
};
}
return report;
}
}