-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.js
More file actions
167 lines (138 loc) · 4.38 KB
/
performance.js
File metadata and controls
167 lines (138 loc) · 4.38 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
// Performance utilities - debouncing, throttling, lazy loading
// Debounce function to limit how often a function can fire
export function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Throttle function to ensure a function is called at most once in a specified time period
export function throttle(func, limit) {
let inThrottle;
return function executedFunction(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// Virtual scroll renderer for large lists
export class VirtualScroller {
constructor(container, items, renderItem, itemHeight = 30) {
this.container = container;
this.items = items;
this.renderItem = renderItem;
this.itemHeight = itemHeight;
this.visibleCount = Math.ceil(container.clientHeight / itemHeight) + 5; // Buffer
this.startIndex = 0;
this.setupContainer();
this.render();
this.attachScrollListener();
}
setupContainer() {
// Create a wrapper for positioning
this.wrapper = document.createElement('div');
this.wrapper.style.position = 'relative';
this.wrapper.style.height = `${this.items.length * this.itemHeight}px`;
// Create viewport for visible items
this.viewport = document.createElement('div');
this.viewport.style.position = 'absolute';
this.viewport.style.top = '0';
this.viewport.style.left = '0';
this.viewport.style.right = '0';
this.wrapper.appendChild(this.viewport);
this.container.appendChild(this.wrapper);
}
render() {
const endIndex = Math.min(this.startIndex + this.visibleCount, this.items.length);
const visibleItems = this.items.slice(this.startIndex, endIndex);
this.viewport.innerHTML = '';
this.viewport.style.transform = `translateY(${this.startIndex * this.itemHeight}px)`;
visibleItems.forEach((item, index) => {
const element = this.renderItem(item, this.startIndex + index);
this.viewport.appendChild(element);
});
}
attachScrollListener() {
const handleScroll = throttle(() => {
const scrollTop = this.container.scrollTop;
const newStartIndex = Math.floor(scrollTop / this.itemHeight);
if (newStartIndex !== this.startIndex) {
this.startIndex = newStartIndex;
this.render();
}
}, 16); // ~60fps
this.container.addEventListener('scroll', handleScroll);
}
update(newItems) {
this.items = newItems;
this.wrapper.style.height = `${this.items.length * this.itemHeight}px`;
this.render();
}
}
// Batch processor for handling large datasets in chunks
export async function processBatch(items, processor, batchSize = 50, onProgress = null) {
const results = [];
const totalBatches = Math.ceil(items.length / batchSize);
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
const batchResults = await processor(batch);
results.push(...batchResults);
if (onProgress) {
const currentBatch = Math.floor(i / batchSize) + 1;
onProgress(currentBatch, totalBatches, results.length);
}
// Yield to main thread
await new Promise(resolve => setTimeout(resolve, 0));
}
return results;
}
// Memory-efficient file reader with progress
export function readFileWithProgress(file, onProgress) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onprogress = (e) => {
if (e.lengthComputable && onProgress) {
const percentComplete = (e.loaded / e.total) * 100;
onProgress(percentComplete);
}
};
reader.onload = (e) => {
resolve(e.target.result);
};
reader.onerror = (e) => {
reject(new Error('File read error'));
};
reader.readAsText(file);
});
}
// Lazy loader for progressive rendering
export class LazyLoader {
constructor(threshold = 500) {
this.threshold = threshold;
this.shouldUseLazy = false;
}
shouldUseLazyLoading(itemCount) {
return itemCount > this.threshold;
}
async loadInChunks(items, chunkSize, processor) {
const chunks = [];
for (let i = 0; i < items.length; i += chunkSize) {
chunks.push(items.slice(i, i + chunkSize));
}
const results = [];
for (const chunk of chunks) {
const chunkResults = await processor(chunk);
results.push(...chunkResults);
// Allow UI to update
await new Promise(resolve => requestAnimationFrame(resolve));
}
return results;
}
}