Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize text listener #985

Merged
merged 2 commits into from
Mar 19, 2019
Merged

refactor: optimize text listener #985

merged 2 commits into from
Mar 19, 2019

Conversation

wssgcg1213
Copy link
Collaborator

重复渲染 1000 次 text 节点, 上屏时间减少 50%

image

@wssgcg1213
Copy link
Collaborator Author

image

@@ -64,7 +65,9 @@ export default class TextElement extends HTMLElement {
}

connectedCallback() {
this.childrenObserver = new FlattenedNodesObserver(this, this._observeChildren);
afterNextRender(this, () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afterNextRender 好像就是 requestFrameCallback

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯 原理上是一样的,但是实现不同.
afterNextRender 会共用同一个 frame 来执行同一个tick触发的callback
如果这里改成 requestAnimationFrame 的话, 实测1000节点70ms左右, 跟原先不优化的效果差不多, 比 setTimeout 也要多 20% 左右的时间(50-60ms).

查了下可能是下面这个原因

浏览器 requestAnimationFrame 的实现比较复杂,需要在 UI 线程注册 vSync 的回调,并且涉及多次进程/线程间的双向通讯

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前大约在 35ms/千节点

@yuanyan yuanyan merged commit d0197f6 into master Mar 19, 2019
@delete-merged-branch delete-merged-branch bot deleted the optimize-atag-text branch March 19, 2019 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants