SlideShare a Scribd company logo
The Virtual DOM
And how React uses it internally
A first look
Let’s see how is the browser workflow, to render a
web page, before we go into deeper details
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_general
Collected from the MDN documentation
The basic data flow
1. The rendering engine will start parsing the HTML document and turn the tags to DOM nodes in a
tree called the "content tree”

2. The styling information together with visual instructions in the HTML will be used to create
another tree - the render tree.

3. After the construction of the render tree it goes through a "layout" process. This means giving
each node the exact coordinates where it should appear on the screen.

4. the render tree will be traversed and each node will be painted using the UI backend layer.
http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_generalData collected from:
Since DOM is represented as a tree structure,
the first render into the DOM is pretty quick!
So, why do we need to use
VDOM?
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
Can you see something in common on the
main browsers workflow after the tree is
processed and attached into the DOM?
Mozilla's Gecko rendering engine main flow
Webkit rendering engine main flow
Can you see some thing in common on the
main browsers workflow, after the tree is
processed e anexada no DOM?
Since DOM is represented as a tree structure, the first
render into the DOM are pretty quick but the changed
element, and its children have to go through Reflow/
Layout stage and then the changes have to be Re-
painted which takes a long time. Therefore the more
items you have to reflow/repaint, the slower your app
becomes.
it's all about rendering
https://www.youtube.com/watch?v=9-ezi9pzdj0
VDOM
"it’s a virtual tree which is kept in the browser memory
and updated in runtime and therefore minimize those
two stages (reflow/repaint) and thereby having a better
performance for a big and complex app”
—Nevinha
How does it work?
When we change something in our Virtual DOM Tree, we get a new
Virtual Tree. The algorithm compares these two trees (old and new),
finds differences and makes only the necessary changes on the DOM,
so at the end it will reflect the virtual DOM.
How is it divided?
1. Node mount representation — React uses JSX and CreateElement
function in order to put it on the virtual tree later. (here) and (here)
2. Mount of the initial virtual tree — It’s possible on web because of
react-dom. (here)
3. Append the VDOM into the DOM — It can be done on the same
function that mounts the virtual tree.
4. For each changes on the virtual tree, the VDOM will compare the
old tree with the new one, in order to see what have been changed
and just re-render the necessary nodes, if and only if it’s necessary.
Node Representation
Basically an element is represented on the VDOM as an object that
contains the following attributes:

• type — The type of the element, which can be a tag name, a function
component (state less) or a class component (state full)
• props or attributes — The attributes of the tag or the custom props
• children — The children that are inside the tag or your component
{
type: "ul",
props: {className: "list"},
children: [
{
type: "li",
props: {},
children: ["item 1"]
},
//{...}
]
}
Stay tuned, it’s react based
https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L312
The next slides will be based on the source code of React.js v16.11.0
which can be founded on the link bellow:
BUT DON’T WORRY ABOUT THE VERSION, IT DOESN'T CHANGE TO
OFFEN 😂
See the node representation that
we’ve just talked few minutes ago?
Mount of the props, if they are custom props, if they
aren’t later react adds then on the element before
append the element into the tree
Also the default props that we define on a
component, they are handled on this function.
Them it returns the a ReactElement ready to use it inside an
existent component or append into the VDOM at the end of the
createElement function
So far…
Right?
Mount of the initial virtual tree
As you already know the basic idea to represent and mount a virtual
node, let’s suppose that we have defined a function called
createElement
Obs: This code doesn’t transpile, because it is pseudo code
As you already have the virtual element creation done, mount
the tree is really easy… you just need to create a function
that get the node representation as argument node and use
recursion to get its virtual children
PS: ignore that we could receive a function or class component on node.type, we will do it later.
Append the VDOM into the DOM
This is the easiest part of the VDOM algorithm, it just uses a function
that takes a component and a container as argument and append the
virtual component, which contains the VTree and append it into the
container.
Obs: This code doesn’t transpile, because it is pseudo code.
Compare changes - DIFF
This is the most important part of the virtual DOM algorithm, it is where
you application get its performance improvement. So basically we
need to write an algorithm, that will compare two virtual trees — old
and new — and make only necessary changes into real DOM.
How react does it?
Actually who is responsible for the DIFF is the react-dom package, on
this link, basically it does diff for the props of a current VTree node,
children and texts.
Let’s see the logic behind the DIFF algorithm that react-dom uses
internally
We will handle the following cases
• There isn’t old node at some place — so node was added and we
need to appendChild(…) that
• There is no new node at some place — thus node was deleted and
we need to removeChild(…)
• Nodes are the same — so we need to go deeper and diff child nodes
We also have the diff of the state and props of a vnode but it won’t be
covered on this presentation.
First things first
We’ll separate a function that does all the magic for us, which we will
call updateElement.
This function will be recursive on the future ;)
There isn’t old node at some place
What is a pretty straightforward operation
There is no new node at some place
Nodes are the same, so we need to go deeper
At this part, we’ll need to adopt the recursion
strategy in order to go deeper into the Tree
First, we’ll create a function that takes two
nodes and see if they are equals
Then we call the changed function inside the update
element function, if the direct children has been
changed, we need to replace the old for the new one
If anything changes between the new and the old
node, it means that we need to go deeper if the new
node is a tag or a component.
The magic is DONE!
The virtual DOM and how react uses it internally
The virtual DOM and how react uses it internally
You can go beyond
Questions?

More Related Content

PPTX
React workshop
PPTX
Introduction to React JS for beginners
PDF
React JS and why it's awesome
PDF
Introduction to React JS
PDF
ReactJS presentation
PPTX
ReactJS presentation.pptx
PDF
React js
ODP
Introduction to ReactJS
React workshop
Introduction to React JS for beginners
React JS and why it's awesome
Introduction to React JS
ReactJS presentation
ReactJS presentation.pptx
React js
Introduction to ReactJS

What's hot (20)

PPTX
React JS - A quick introduction tutorial
PPTX
React-JS Component Life-cycle Methods
PPTX
Introduction to React JS
PDF
React new features and intro to Hooks
PPTX
reactJS
PDF
PPTX
Intro to React
PPTX
Intro to React
PPTX
React js programming concept
PPTX
Introduction to React JS for beginners | Namespace IT
PPTX
Its time to React.js
PPTX
React js for beginners
PDF
React JS - Introduction
PDF
An Introduction to ReactJS
PPTX
[Final] ReactJS presentation
PDF
WEB DEVELOPMENT USING REACT JS
PDF
Basics of React Hooks.pptx.pdf
PPTX
React Hooks
PDF
ES6 presentation
React JS - A quick introduction tutorial
React-JS Component Life-cycle Methods
Introduction to React JS
React new features and intro to Hooks
reactJS
Intro to React
Intro to React
React js programming concept
Introduction to React JS for beginners | Namespace IT
Its time to React.js
React js for beginners
React JS - Introduction
An Introduction to ReactJS
[Final] ReactJS presentation
WEB DEVELOPMENT USING REACT JS
Basics of React Hooks.pptx.pdf
React Hooks
ES6 presentation
Ad

Similar to The virtual DOM and how react uses it internally (20)

PPTX
What is virtual dom in react js
PDF
Welcome to React & Flux !
PPTX
React js - The Core Concepts
PPTX
Introduction to React JS.pptx
PDF
Getting Started with React, When You’re an Angular Developer
PPTX
Introduction to ReactJS UI Web Dev .pptx
PDF
Learn reactjs, how to code with example and general understanding thinkwik
PDF
Fundamental concepts of react js
PDF
Full Stack React Workshop [CSSC x GDSC]
PDF
React Interview Questions for Noobs or Juniors
PPT
Web Performance Tips
PDF
Tech Talk on ReactJS
PPTX
React patterns
PPTX
What are the components in React?
PPTX
React JS Interview Question & Answer
PDF
React JS Interview Questions PDF By ScholarHat
PPTX
Better web apps with React and Redux
PDF
Vue.js basics
PDF
JOSA TechTalks - Better Web Apps with React and Redux
PDF
The complete-beginners-guide-to-react dyrr
What is virtual dom in react js
Welcome to React & Flux !
React js - The Core Concepts
Introduction to React JS.pptx
Getting Started with React, When You’re an Angular Developer
Introduction to ReactJS UI Web Dev .pptx
Learn reactjs, how to code with example and general understanding thinkwik
Fundamental concepts of react js
Full Stack React Workshop [CSSC x GDSC]
React Interview Questions for Noobs or Juniors
Web Performance Tips
Tech Talk on ReactJS
React patterns
What are the components in React?
React JS Interview Question & Answer
React JS Interview Questions PDF By ScholarHat
Better web apps with React and Redux
Vue.js basics
JOSA TechTalks - Better Web Apps with React and Redux
The complete-beginners-guide-to-react dyrr
Ad

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
Review of recent advances in non-invasive hemoglobin estimation
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Monthly Chronicles - July 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

The virtual DOM and how react uses it internally

  • 1. The Virtual DOM And how React uses it internally
  • 2. A first look Let’s see how is the browser workflow, to render a web page, before we go into deeper details Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_general Collected from the MDN documentation
  • 3. The basic data flow 1. The rendering engine will start parsing the HTML document and turn the tags to DOM nodes in a tree called the "content tree” 2. The styling information together with visual instructions in the HTML will be used to create another tree - the render tree. 3. After the construction of the render tree it goes through a "layout" process. This means giving each node the exact coordinates where it should appear on the screen. 4. the render tree will be traversed and each node will be painted using the UI backend layer. http://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_generalData collected from:
  • 4. Since DOM is represented as a tree structure, the first render into the DOM is pretty quick!
  • 5. So, why do we need to use VDOM?
  • 6. Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow Can you see something in common on the main browsers workflow after the tree is processed and attached into the DOM?
  • 7. Mozilla's Gecko rendering engine main flow Webkit rendering engine main flow Can you see some thing in common on the main browsers workflow, after the tree is processed e anexada no DOM?
  • 8. Since DOM is represented as a tree structure, the first render into the DOM are pretty quick but the changed element, and its children have to go through Reflow/ Layout stage and then the changes have to be Re- painted which takes a long time. Therefore the more items you have to reflow/repaint, the slower your app becomes.
  • 9. it's all about rendering https://www.youtube.com/watch?v=9-ezi9pzdj0
  • 10. VDOM "it’s a virtual tree which is kept in the browser memory and updated in runtime and therefore minimize those two stages (reflow/repaint) and thereby having a better performance for a big and complex app” —Nevinha
  • 11. How does it work? When we change something in our Virtual DOM Tree, we get a new Virtual Tree. The algorithm compares these two trees (old and new), finds differences and makes only the necessary changes on the DOM, so at the end it will reflect the virtual DOM.
  • 12. How is it divided? 1. Node mount representation — React uses JSX and CreateElement function in order to put it on the virtual tree later. (here) and (here) 2. Mount of the initial virtual tree — It’s possible on web because of react-dom. (here) 3. Append the VDOM into the DOM — It can be done on the same function that mounts the virtual tree. 4. For each changes on the virtual tree, the VDOM will compare the old tree with the new one, in order to see what have been changed and just re-render the necessary nodes, if and only if it’s necessary.
  • 13. Node Representation Basically an element is represented on the VDOM as an object that contains the following attributes:
 • type — The type of the element, which can be a tag name, a function component (state less) or a class component (state full) • props or attributes — The attributes of the tag or the custom props • children — The children that are inside the tag or your component
  • 14. { type: "ul", props: {className: "list"}, children: [ { type: "li", props: {}, children: ["item 1"] }, //{...} ] }
  • 15. Stay tuned, it’s react based https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L312 The next slides will be based on the source code of React.js v16.11.0 which can be founded on the link bellow:
  • 16. BUT DON’T WORRY ABOUT THE VERSION, IT DOESN'T CHANGE TO OFFEN 😂
  • 17. See the node representation that we’ve just talked few minutes ago?
  • 18. Mount of the props, if they are custom props, if they aren’t later react adds then on the element before append the element into the tree
  • 19. Also the default props that we define on a component, they are handled on this function.
  • 20. Them it returns the a ReactElement ready to use it inside an existent component or append into the VDOM at the end of the createElement function
  • 22. Mount of the initial virtual tree As you already know the basic idea to represent and mount a virtual node, let’s suppose that we have defined a function called createElement Obs: This code doesn’t transpile, because it is pseudo code
  • 23. As you already have the virtual element creation done, mount the tree is really easy… you just need to create a function that get the node representation as argument node and use recursion to get its virtual children PS: ignore that we could receive a function or class component on node.type, we will do it later.
  • 24. Append the VDOM into the DOM This is the easiest part of the VDOM algorithm, it just uses a function that takes a component and a container as argument and append the virtual component, which contains the VTree and append it into the container.
  • 25. Obs: This code doesn’t transpile, because it is pseudo code.
  • 26. Compare changes - DIFF This is the most important part of the virtual DOM algorithm, it is where you application get its performance improvement. So basically we need to write an algorithm, that will compare two virtual trees — old and new — and make only necessary changes into real DOM.
  • 27. How react does it? Actually who is responsible for the DIFF is the react-dom package, on this link, basically it does diff for the props of a current VTree node, children and texts. Let’s see the logic behind the DIFF algorithm that react-dom uses internally
  • 28. We will handle the following cases • There isn’t old node at some place — so node was added and we need to appendChild(…) that • There is no new node at some place — thus node was deleted and we need to removeChild(…) • Nodes are the same — so we need to go deeper and diff child nodes We also have the diff of the state and props of a vnode but it won’t be covered on this presentation.
  • 29. First things first We’ll separate a function that does all the magic for us, which we will call updateElement. This function will be recursive on the future ;)
  • 30. There isn’t old node at some place What is a pretty straightforward operation
  • 31. There is no new node at some place
  • 32. Nodes are the same, so we need to go deeper At this part, we’ll need to adopt the recursion strategy in order to go deeper into the Tree
  • 33. First, we’ll create a function that takes two nodes and see if they are equals
  • 34. Then we call the changed function inside the update element function, if the direct children has been changed, we need to replace the old for the new one
  • 35. If anything changes between the new and the old node, it means that we need to go deeper if the new node is a tag or a component.
  • 36. The magic is DONE!
  • 39. You can go beyond