perf: optimize tag badge generation in ui_helpers.js
Replaced redundant Array traversal (map().join('')) with a for...of loop
to construct the tagsHtml string. This avoids allocating a new array
for each item being rendered, reducing memory pressure and improving
rendering performance for menus with many tags.
Benchmark results (100,000 iterations):
- 0 tags: 13.3ms -> 4.0ms (~70% improvement)
- 1 tag: 60.9ms -> 46.7ms (~23% improvement)
- 10 tags: 489ms -> 411ms (~16% improvement)
- 50 tags: 2286ms -> 1942ms (~15% improvement)
Co-authored-by: TauNeutrino <1600410+TauNeutrino@users.noreply.github.com>
This commit is contained in:
52
tests/benchmark_tags.js
Normal file
52
tests/benchmark_tags.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
const { performance } = require('perf_hooks');
|
||||
|
||||
function escapeHtml(text) {
|
||||
// Simple mock for benchmark purposes
|
||||
return text
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function currentImplementation(matchedTags) {
|
||||
const badges = matchedTags.map(t => `<span class="tag-badge-small"><span class="material-icons-round" style="font-size:10px;margin-right:2px">star</span>${escapeHtml(t)}</span>`).join('');
|
||||
return `<div class="matched-tags">${badges}</div>`;
|
||||
}
|
||||
|
||||
function optimizedImplementation(matchedTags) {
|
||||
let badges = '';
|
||||
for (const t of matchedTags) {
|
||||
badges += `<span class="tag-badge-small"><span class="material-icons-round" style="font-size:10px;margin-right:2px">star</span>${escapeHtml(t)}</span>`;
|
||||
}
|
||||
return `<div class="matched-tags">${badges}</div>`;
|
||||
}
|
||||
|
||||
const tagSizes = [0, 1, 5, 10, 50];
|
||||
const iterations = 100000;
|
||||
|
||||
console.log(`Running benchmark with ${iterations} iterations...`);
|
||||
|
||||
for (const size of tagSizes) {
|
||||
const tags = Array.from({ length: size }, (_, i) => `Tag ${i}`);
|
||||
|
||||
console.log(`\nTag count: ${size}`);
|
||||
|
||||
// Baseline
|
||||
const startBaseline = performance.now();
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
currentImplementation(tags);
|
||||
}
|
||||
const endBaseline = performance.now();
|
||||
console.log(`Baseline (map.join): ${(endBaseline - startBaseline).toFixed(4)}ms`);
|
||||
|
||||
// Optimized
|
||||
const startOptimized = performance.now();
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
optimizedImplementation(tags);
|
||||
}
|
||||
const endOptimized = performance.now();
|
||||
console.log(`Optimized (for...of): ${(endOptimized - startOptimized).toFixed(4)}ms`);
|
||||
}
|
||||
Reference in New Issue
Block a user