简体   繁体   English

React.js如何加速虚拟DOM的渲染

[英]How React.js speeds up rendering with a virtual DOM

Quoting this ( https://news.ycombinator.com/item?id=9155564 ) article 引用此文章( https://news.ycombinator.com/item?id=9155564

The short answer is that the DOM is not slow. 简短的回答是DOM并不慢。 Adding & removing a DOM node is a few pointer swaps, not much more than setting a property on the JS object. 添加和删​​除DOM节点是一些指针交换,只不过是在JS对象上设置属性。

Are the DOM bottlenecks only those things that cause a redraw? DOM的瓶颈只是导致重绘的东西吗? If so then shouldn't one render from React's virtual DOM amortize to the same performance as redrawing an entire component (in one browser API call of course)? 如果是这样,那么不应该从React的虚拟DOM渲染渲染到与重绘整个组件相同的性能(当然在一个浏览器API调用中)? I would think that the algorithms executed by the browser only try and redraw the diff from one state to another (like git maybe?). 我认为浏览器执行的算法只会尝试将差异从一个状态重绘为另一个状态(比如git可能?)。 Implying that the browser maintains a virtual DOM by itself. 暗示浏览器自己维护虚拟DOM。 So then what is the point of having a virtual DOM? 那么拥有虚拟DOM有什么意义呢?

Also should adding an element that has the display style property set to none not be affecting performance badly? 还应该添加一个display样式属性设置为none的元素不会严重影响性能吗? I would profile this myself but I do not know where exactly to turn as I started javascript programming only recently. 我自己会对此进行分析,但我不知道到底在哪里转向,因为我最近才启动javascript编程。

This question may be somewhat broad for SO, but as a general answer, some other quotes from the same article are also very relevant: 对于SO来说,这个问题可能有点宽泛,但作为一般性答案,同一篇文章中的其他一些引用也非常相关:

However, layout is slow... 但是布局很慢......
[...] [...]
Worse, layout is triggered synchronously by accessing certain properties... 更糟糕的是,通过访问某些属性来同步触发布局......
[...] [...]
Because of this, a lot of Angular and JQuery code is stupidly slow 因此,很多Angular和JQuery代码都很愚蠢
[...] [...]
React doesn't help speed up layout... React无助于加快布局......

What react's virtual DOM does, is calculate differences between one state of the DOM and the next state and minimizes DOM updates in a very smart way . 反应的虚拟DOM的作用是计算DOM的一个状态与下一个状态之间的差异,并以非常智能的方式最小化DOM更新

So: 所以:

  • DOM itself is not slow DOM本身并不慢
  • but layout is slow 但布局很慢
  • and almost all DOM updates require layout updates 几乎所有DOM更新都需要布局更新
  • so less DOM updates is faster 所以更少的DOM更新更快

And the react engine does just that (same as several other tools/ libraries with a virtual DOM). 并且反应引擎就是这样(与具有虚拟DOM的其他几个工具/库相同)。

More info on what virtual DOM is and its advantages eg here . 有关虚拟DOM的更多信息及其优势,例如此处


Q: "Are the DOM bottlenecks only those things that cause a redraw?" 问: “DOM的瓶颈只是导致重绘的东西吗?”


A: The redraw is GPU dependent. 答:重绘是依赖于GPU的。 Has nothing to do with the speed od DOM updates. 与DOM更新的速度无关。 DOM updates are almost instant. DOM更新几乎是即时的。 Everything depends on changes that do affect the document flow. 一切都取决于影响文档流程的变化。 If a certain DOM or DHTML change affects the document flow. 如果某个DOM或DHTML更改影响文档流。 The closer the affected element is to the root of the document element the greater the impact on the document reflow. 受影响的元素越接近文档元素的根,对文档重排的影响就越大。

You don't need to change the DOM content in order to cause a document reflow. 您无需更改DOM内容即可导致文档重排。 A simple style property change on a given parameter may push elements of the stream to change position and cause therefore force the document reflow. 对给定参数的简单样式属性更改可能会推送流的元素以更改位置,从而导致文档重排。

Therefore no, DOM changes on fixed size Elements will not cause a document reflow, whereas the update of display is practically instant. 因此,固定大小的DOM上的DOM更改不会导致文档重排,而显示的更新几乎是即时的。 Will be applied only on locally affected area, most of the time in a frame which may be less than 300 x 200 pixels square; 将仅应用于局部受影响的区域,大多数时间在一个可能小于300 x 200像素的帧中; a size of an area that can be redrawn with over 120fps on a really slow GPU's. 在非常慢的GPU上可以重新绘制超过120fps的区域大小。 But that's 5 times smoother than watching Avengers in Cinema. 但这比在电影院观看复仇者联盟要轻松5倍。

( Any spatially nonequivalent change in stream-aligned content will cause a reflow. So we have to watch for changes that affect the size and position of our floating elements, changes on inline elements inside a long stream of another inline element, etc, etc. ) (流对齐内容中的任何空间非等效变化都将导致重排。因此,我们必须注意影响浮动元素的大小和位置的变化,内部元素在另一个内联元素的长流内的变化等。 )

'


Q: "should adding an element that has the display style property set to none not be affecting performance badly?" 问: “应该添加一个显示样式属性设置为none的元素不会严重影响性能吗?”


A: That's correct. A:没错。 Adding an element with style.display: "none" to the DOM will cause no change to the existing rendering of the document, and therefore, not trigger a document reflow and will, naturally, have no impact at all; 向DOM添加带有style.display:“none”的元素将不会对文档的现有呈现进行任何更改,因此,不会触发文档重排,并且自然不会产生任何影响; ie: will be as fast as adding a new property to a JavaScript object. 即:将与向JavaScript对象添加新属性一样快。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM