dom-performance
它能做什么
这项技能查找不断读取和修改布局的页面代码,这会强制进行缓慢的重新计算。适合希望页面流畅、响应迅速的开发者。
适用场景
- ✓当页面在动画或更新期间短暂卡住时。
- ✓当脚本在循环中更新许多元素时。
- ✓当滚动或触摸操作感觉迟钝时。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/dom-performance/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“你能检查我的代码中是否有导致页面更新缓慢的步骤吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出读取和写入页面布局混合在一起的地方,建议先统一读取、再统一写入,并把部分滚动监听器标记为 passive。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Minimize costly DOM read/write operations
Every time you read a layout property after making a DOM change, the browser must synchronously recalculate layout (reflow) before returning the value. Doing this in a loop — even with just 10 elements — can freeze the page for hundreds of milliseconds. This is called layout thrashing and is one of the most common causes of janky animations and slow renders.
Quick Reference
- Never interleave DOM reads (offsetHeight, getBoundingClientRect) with DOM writes (style changes)
- Batch all reads first, then all writes
- Use requestAnimationFrame for visual updates
- Use DocumentFragment or innerHTML for bulk DOM insertion
- Avoid per-frame React state updates across many visible components when a simpler CSS or reduced-frequency approach would work
- Use passive listeners for scroll, touch, and wheel when you do not call preventDefault
Check
Identify any layout thrashing in this code — places where DOM reads and writes are interleaved in loops. Also flag scroll, touch, and wheel listeners that should be passive to avoid blocking the main thread.
Fix
Refactor this code to batch all DOM reads before writes, eliminating forced synchronous layouts. Mark relevant listeners as passive when they do not need to cancel scrolling.
Explain
Explain layout thrashing, what causes it, and how batching reads and writes prevents it.
Code Review
Review scripts, client components, and browser execution paths related to Minimize costly DOM read/write operations. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/javascript/dom-performance
常见问题
什么是布局抖动(layout thrashing)?
指浏览器在一次更新中被迫反复重新计算布局,这可能使页面短暂卡住。
什么是 passive 监听器?
它告诉浏览器,滚动或触摸处理程序不会阻止滚动,从而使滚动保持流畅。
它涵盖 React 的状态更新吗?
这项技能说明,如果代码中可见,许多组件中基于帧的重复状态更新可能会成为性能问题。
详情
作者的原始描述
Use when reviewing scripts, client components, bundles, or runtime behavior related to Minimize costly DOM read/write operations. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug. In React, repeated requestAnimationFrame-driven state updates across multiple mounted components are a valid performance smell when visible in the source.
热度
- GitHub 星标
- 75k
- 排名
- #2820
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。