1

I read through all the articles in internet. Still i cant understand and mind is puzzling me questions again and again that is

At the end of all the virtual DOM it is going to call the browser API to update the real DOM then how come it will be faster?

Is virtual DOM of React have special access to browsers core API's to modify?

I cant understand? Any resolves this questions Thanks in advance

2
  • The virtual DOM is 'faster' because it 1) batches all DOM modifications and accesses at once, assuming you use the appropriate APIs and 2) calculates a diff on what needs to be changed, added and removed so that in theory it modifies the DOM more efficiently than a programmer writing the modifications by hand
    – Dan
    Commented Dec 30, 2019 at 4:40
  • Anyway i am also going to call dom apis only for the case of what needs to be changed right?
    – Kumar Raj
    Commented Dec 30, 2019 at 4:48

3 Answers 3

1

Here is a talk given circa 2013 (v0.4.0) by the two guys behind React. They describe exactly how it works. Unlike data binding and dirty checking (Angular, etc.) React uses one render method that's called recursively. It then generates a long string that is a representation of the DOM. The concept is actually really simple.

https://www.youtube.com/watch?v=XxVg_s8xAms

1

Yes, you are right finally the task is to update real dom but the virtual dom comes in picture before updating the real dom. how ?

Suppose you want to update any/many element(s) in Dom tree element then there should be a mechanism to find which element(s) needs to be updated in real dom i.e the browser screen we see.

So this dom finding algorithm executed in virtual dom i.e a javascript copy of real dom(a html dom tree).

React creates two virtual dom, one from existing real dom and other from the changes made. These two virtual dom comparison saves time. The difference of this comparison used for updating real dom.

0

At the end of all the virtual DOM it is going to call the browser API to update the real DOM then how come it will be faster?

Any speed benefits come from minimizing the number of DOM manipulations that are needed and doing them all at once. The virtual DOM is react's way of calculating the minimum set of changes.

Here's what i mean by calculating the minimum set of changes: The page starts off looking one way, and then you want to make it look some other way. To get there, you're going to need to make one or more changes to the DOM, but there are many different ways you could do it.

A really bad way to update the page would be to wipe the entire document, and then rebuild every single element from scratch. Most likely though, you can reuse most of the page and just make a few updates to select parts of the page: add a div here; update a property there, add an event listener there. That's what you want: a small number of steps that take the old page and turn it into the new page.

3
  • Calculating Minimum set of changes? Could you provide best example?
    – Kumar Raj
    Commented Dec 30, 2019 at 4:46
  • I think this question is a little too broad. The React docs have a page that details how the Virtual DOM (and Reconciliation) works. You should read those to understand exactly how React works reactjs.org/docs/faq-internals.html
    – Dan
    Commented Dec 30, 2019 at 4:51
  • @KumarRaj i have added a description Commented Dec 30, 2019 at 4:54

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