功能建议:多项性能优化方案

Open
#756 2 comments 4 reactions 0 assignees View on GitHub

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:

实现:改为 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from maboloshi/github-chinese

All issues in maboloshi/github-chinese

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.