Skip to content

Commit 37bd655

Browse files
committed
perf(webview-ui): add LRU cache to escapeHtml function
- Add LRU cache with 500 item limit for escapeHtml results - Improves performance by caching frequently escaped strings - Reduces redundant HTML escaping operations
1 parent 4322529 commit 37bd655

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

webview-ui/src/utils/highlight.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
import { LRUCache } from "lru-cache"
2+
3+
// LRU cache for escapeHtml with reasonable size limit
4+
const escapeHtmlCache = new LRUCache<string, string>({ max: 500 })
5+
16
function escapeHtml(text: string): string {
2-
return text
7+
// Check cache first
8+
const cached = escapeHtmlCache.get(text)
9+
if (cached !== undefined) {
10+
return cached
11+
}
12+
13+
// Compute escaped text
14+
const escaped = text
315
.replace(/&/g, "&amp;")
416
.replace(/</g, "&lt;")
517
.replace(/>/g, "&gt;")
618
.replace(/"/g, "&quot;")
719
.replace(/'/g, "&#39;")
20+
21+
// Cache the result
22+
escapeHtmlCache.set(text, escaped)
23+
24+
return escaped
825
}
926

1027
export function highlightFzfMatch(

0 commit comments

Comments
 (0)