2

Quoting this (https://news.ycombinator.com/item?id=9155564) article

The short answer is that the DOM is not slow. Adding & removing a DOM node is a few pointer swaps, not much more than setting a property on the JS object.

Are the DOM bottlenecks only those things that cause a redraw? 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)? I would think that the algorithms executed by the browser only try and redraw the diff from one state to another (like git maybe?). Implying that the browser maintains a virtual DOM by itself. So then what is the point of having a virtual DOM?

Also should adding an element that has the display style property set to none not be affecting performance badly? I would profile this myself but I do not know where exactly to turn as I started javascript programming only recently.

10
  • 1
    if you draw only once, nothing is slow.
    – YOU
    Commented May 11, 2016 at 10:41
  • That is what I am trying to ask, if you render n times with a virtual DOM ($().prepend or maybe $.append()) as opposed to n browser API calls should the difference be huge?
    – Curious
    Commented May 11, 2016 at 10:44
  • its prepend or append when needed, and just change attribute or only change one of its descendent, all based on how data changed and diff algorithm
    – YOU
    Commented May 11, 2016 at 10:49
  • 1
    if chrome do that, it will be big news about that.
    – YOU
    Commented May 11, 2016 at 10:53
  • 1
    @YOU That's okay! If it is for your company I probably have no business looking at it! I was just curious. Thanks though!
    – Curious
    Commented May 11, 2016 at 11:05

2 Answers 2

5

This question may be somewhat broad for SO, but as a general answer, some other quotes from the same article are also very relevant:

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
[...]
React doesn't help speed up layout...

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.

So:

  • DOM itself is not slow
  • but layout is slow
  • and almost all DOM updates require layout updates
  • so less DOM updates is faster

And the react engine does just that (same as several other tools/ libraries with a virtual DOM).

More info on what virtual DOM is and its advantages e.g. here.

2
  • Yep! I understand, but if say I have a div that has all the html for the page, and then I update that div entirely. That is also one update. And if react goes and calculates the diff itself. That would also be only one update. What prevents the browser from calculating the diff between an old and a new state itself?
    – Curious
    Commented May 11, 2016 at 10:58
  • Usually, the <div> contains a very elaborate HTML-tree, with many elements. If you replace the entire tree, that 1 update actually unpacks to maybe thousands of sub-updates. What react does, is only do the necessary sub-updates. If you replace the entire tree, and every single sub-component is also different, than react's difference engine has no effect. But in real life, this is hardly ever the case. AFAIK browser do not do diff like react does. What "prevents" them is a matter of browser DEV team development priorities I guess.
    – wintvelt
    Commented May 11, 2016 at 11:19
0

Q: "Are the DOM bottlenecks only those things that cause a redraw?"


A: The redraw is GPU dependent. Has nothing to do with the speed od DOM updates. DOM updates are almost instant. Everything depends on changes that do affect the document flow. If a certain DOM or DHTML change affects the document flow. 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. 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. 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; a size of an area that can be redrawn with over 120fps on a really slow GPU's. But that's 5 times smoother than watching Avengers in Cinema.

( 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?"


A: That's correct. 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; i.e.: will be as fast as adding a new property to a JavaScript object.

Regards.

Not the answer you're looking for? Browse other questions tagged or ask your own question.