Sandip Subedi
Sandip Subedi

Reputation: 1077

What actually makes update using React faster that regular UI update?

I have already read 10 articles about React and Virtual DOM.

I understand that virtual DOM uses the diffing algorithm and only update the UI that was changed. But I still don't understand why that is faster than updating the actual DOM.

Following is an example:

<div id="test">
    Hello React!
</div>

Let's say we created a component and changed it using React. Let's say we changed the text to Hello World!

I can do the same thing using plain JS right ? document.getElementById('test').innerHTML = Hello World!

My question:

Why is React faster ? I feel like React is doing exactly same thing under the hood right ?

I feel like I am missing something fundamental here.

Upvotes: 2

Views: 1342

Answers (6)

Huy Nguyen
Huy Nguyen

Reputation: 3010

A few reasons off the top of my head:

  • React uses a virtual DOM, which are just JS objects, to represent the DOM. The "current" version of the virtual DOM are objects with references to the actual DOM elements while the "next" vDOM are just objects. Objects are incredibly fast to manipulate because they are just memory changes whereas real DOM changes require expensive style layout, paint and rasterization steps.
  • React diffs the current vDOM against the next vDOM to produce the smallest number of changes required to make the real DOM reflect the next vDOM. This is called reconciliation. The fewer changes you make to the DOM, the faster layout calculations will be.
  • React batches DOM changes together so that it touches the real DOM as few times as possible. It also uses requestAnimationFrame everywhere to ensure that real DOM changes play "nicely" with the browser's layout calculation cycles.
  • Finally (probably React's least appreciated feature), React has increasingly sophisticated scheduling step to distinguish between low- and high-priority updates. Low priority updates are UI updates that can afford to take longer e.g. data fetched from servers whereas high-priority updates are things that the user will notice right away e.g. user input fields. Low priority updates use the very new requestIdleCalback API to ensure that they run when the browser's main thread is actually idle and that they frequently yield back to the main thread to avoid locking up the UI.

Upvotes: 1

Tom Mendelson
Tom Mendelson

Reputation: 625

You might just use vanilla-js as you described:

document.getElementById('test').innerHTML = Hello World!

that's great, but super hard for medium \ big projects.

why? because react handle all your dom interaction and minmize it (as much as it could), when you would use vanilla-js most of your code would be just manipulation on the dom, extract data and insert data, with react you can put those worries aside and put all your afforts to create the best site\project.

more over, the virtual dom makes all the calculation behinde the sense, when you would try to do it manulay you have to deal with all the calculation every time (when you update an list and when you update another one), most probably from some point most of your code would be calculation about the dom updates sound familiar? well, just for that you already got react.

Don't Repeat Yourself

if someone already done it, reuse it.

Separation Of Concerns

one part of your project should manipulate the ui, another should manipulate the logic

and the list can go on and on, but in conclusion the most important thing, vanilla-js is faster, in the end with the virtual dom and without it, you would have to use vanilla-js in the end. make your life simpler, make your code readable.

Upvotes: 0

c-smile
c-smile

Reputation: 27460

why that is faster than updating the actual DOM.

It is not faster. Explicit and controllable DOM updates are faster than anything else.

React may schedule better update graphs on sites similar to facebook but with the cost of diff processing O(D*N). On other sites React could be just a waste of CPU power.

No silver bullet here - each framework is optimal for the site it was created for initially. For others you will be lucky if particular framework is at least sub-optimal.

Real and complex Web App UIs are not using any "framework" but their own primitives: GMail, Google Docs, etc.

Upvotes: 0

Abslen Char
Abslen Char

Reputation: 3135

Actually the Virtual Dom is not faster than the actual Dom , The real DOM itself is fast, it can search, remove and modify elements from the DOM tree quickly. However the layout and painting elements in html tree is slow. But React virtual DOM is not faster. The real benefit from Virtual DOM is it allows calculation the different between each changes and make make minimal changes into the HTML document.

Now why react is better when it come to manipulating the DOM?,your browser does a lot of work to update the DOM. Changing the DOM can trigger reflows and repaints; when one thing changes, the browser has to re-calculate the position of other elements in the flow of the page, and also has to do work re-drawing.

The browser has its own internal optimization to reduce the impact of DOM changes (e.g. doing repaints on the GPU, isolating some repaints on their own layers, etc), but broadly speaking, changing a few things can trigger expensive reflows and repaints.

It's common even when writing everything from scratch to build UI off the DOM, then insert it all at once (e.g. document.createElement a div and insert a whole tree under it for attaching to the main DOM), but React is engineered to watch changes and intelligently update small parts of the DOM to minimize the impact of reflows and repaints

Upvotes: 2

MadPapo
MadPapo

Reputation: 495

React is NOT faster then PURE Javascript. The big difference between them is :

Pure Javascript: if you can master the Javascript language and have time to spend to find the best solution (with the lowest impact in browser performance) you definitively got an UI render system more powerful then React (because you have created a specific engine oriented to your necessity)

React: if you want to spend more time in data structure with no worries on UI update performance React (or Vue.js the future candidate for UI developing) is the best choice

Upvotes: -1

Jonas Wilms
Jonas Wilms

Reputation: 138247

In your case the plain js function will be definetly faster. React is just very good for really complicated UIs. The more complicated your UI gets, you either need to write a lot code to update it or you just rebuild the whole UI on every rerender. However those DOM updates are quite slow. React allows you to completely rerender your data but actually not rerender the whole DOM but just update some parts of it.

Upvotes: 2

Related Questions