perf(transformers): optimize transformerCompactLineOptions lookup to O(1)
### **Description**
The `transformerCompactLineOptions` transformer currently performs an `Array.find()` lookup for every line of highlighted code. This means the transformer repeatedly scans the entire `lineOptions` array, which becomes increasingly expensive for large files.
### Current Behavior
In `packages/transformers/src/transformers/compact-line-options.ts`:
```ts
line(node, line) {
// This .find() runs N times (where N is lines of code)
// scanning M options each time.
const lineOption = lineOptions.find(o => o.line === line)
if (lineOption?.classes)
this.addClassToHast(node, lineOption.classes)
return node
},
```
### Why This Matters
When rendering long code blocks (e.g., 500–2000+ lines), especially in static site generators such as VitePress, Astro, Nuxt Content, etc., this becomes a noticeable performance cost.
Since this transformer exists to support legacy `lineOptions`, optimizing it prevents slowdowns for users who still rely on that API.
This results in a time complexity of **O(N \* M)**, which can degrade performance significantly when rendering large files with many line options.
### Proposed Solution
Refactor the transformer to pre-compute the options into a `Map` during initialization.
1. **Initialization:** Iterate options once to build a Map -> **O(M)**.
2. **Execution:** O(1) lookup per line during the highlighting phase -> **O(N)**.
3. **Total Complexity:** **O(M + N)**.
关闭于 2025-12-04 0 条评论