功能建议:多项性能优化方案
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- javascript
- Domain
- tooling
Research direction
The issue describes five optimizations but names no source files or tests. Start by locating CONFIG, buildPageConfig(), fetchTransResult(), the TreeWalker scan, regex matching, and OBSERVER_CONFIG in the userscript; check how each currently works before evaluating the proposed changes. Done means the five optimizations are implemented and verified, including the stated fallback and cache clearing behavior.
Written by the indexing model from the issue text.
Description
优化 1:页面组回退机制
可能在多个页面类型(repository/actions、repository/tree、repository)中出现,目前需要在各 pageType 的 static 下分别添加,维护成本高。
实现:在 CONFIG 中增加 pageGroupFallback 配置,指定 pageType 之间的回退链。buildPageConfig() 合并静态词典时按 public → 回退页 → 当前页 优先级合并。已在本地实现并验证。
优化 2:翻译缓存
每次文本翻译都要遍历正则规则列表,同一文本在页面重复出现(如按钮标签)会重复遍历。
实现:在 fetchTransResult() 中增加 Map 缓存,首次匹配后缓存结果,后续相同文本直接 O(1) 返回。翻页时清空缓存。
优化 3:TreeWalker 改为 querySelectorAll 定向扫描
当前使用 createTreeWalker 遍历整个 DOM 树,大量无翻译价值的节点也被遍历。
参考 benchmark:
- https://benchmarklab.azurewebsites.net/Benchmarks/ShowResult/615410(querySelectorAll 比 TreeWalker 快约 2 倍)
- https://measurethat.net/Benchmarks/ShowResult/581071
实现:改为 rootNode.querySelectorAll() 只选择翻译目标标签(a、span、button、label、h1~h6、li、td、th、div、p 等),利用浏览器原生 CSS 选择引擎。极端情况回退 TreeWalker。
优化 4:正则循环加速——触发词过滤 + 首字母分组
当前正则循环线性遍历全部规则(部分页面 150+ 条),每条文本都从头试到尾。不匹配任何正则的文本(如文件名、hash)也需完整遍历。
实现:从每条正则中提取触发词(Deploy、Build 等),构建 triggerWords Set,文本不含任何触发词时直接跳过正循环;将正则按触发词首字母分组(D → [Deploy..., Docker...]),文本以某字母开头只查对应组。
优化 5:移除 characterData 减少无效触发
当前 OBSERVER_CONFIG 启用 characterData: true,翻译文本后设置 node.data 会再次触发 MutationObserver,导致已翻译节点被二次处理。
实现:移除 characterData,仅监听 childList + attributes。新增内容通过 childList 自然被捕获。
- Dominant language
- JavaScript
- Stars
- 35.1k
- Forks
- 2k
- Avg merge
- 2d 1h
- Merged PRs (30d)
- 9
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from maboloshi/github-chinese
-
添加报告文档问题的模板Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
maboloshi/github-chinese#823 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 25/100
maboloshi/github-chinese#810 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 44/100
maboloshi/github-chinese#809 · 2 comments ·
Maintainers usually reply within 1 day
-
项目首页除了顶栏几乎没有翻译Openbug
Difficulty 3/5 1-2 days Newbie friendliness 55/100
maboloshi/github-chinese#803 · 1 comment · 2 reactions ·
Maintainers usually reply within 1 day
-
请求翻译
Difficulty 3/5 1-2 days Newbie friendliness 55/100
maboloshi/github-chinese#802 · 2 comments ·
Maintainers usually reply within 1 day
All issues in maboloshi/github-chinese
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
postcss/postcss.org#302 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ImranR98/apps.obtainium.imranr.dev#2000 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
microsoft/monaco-editor#5577 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Ebony-Vinyl/dsh-our-free-model#785 ·
Maintainers usually reply within 1 day