Virtual DOM vs DOM
People think the virtual DOM is a faster DOM that the browser has, and it is not. The DOM is the real page the browser draws. The virtual DOM is a copy a library like React keeps in memory. The library compares two copies and then changes the real DOM as little as it can.
Virtual DOM
A copy of the page kept in memory. The app checks two copies and fixes only what changed.
A virtual DOM is a light copy of the page that a UI library keeps in memory. When data changes, the library builds a new copy. It compares the new copy with the old one to find what is different. Then it changes only those parts of the real page. Changing the real page is slow, so doing it less keeps the app fast. React and Vue both work this way.
Document Object Model (DOM)
The browser's live tree of a page. JavaScript can read it and change it.
The DOM is the browser's live copy of a web page, held as a tree of parts. When a page loads, the browser turns the HTML into this tree. JavaScript can then find any part, change its text, add new parts or remove old ones. The page on screen updates to match. Every interactive web page works by changing the DOM.
In one line
- Virtual DOM
- A copy of the page kept in memory. The app checks two copies and fixes only what changed.
- Document Object Model (DOM)
- The browser's live tree of a page. JavaScript can read it and change it.