This article was originally published at https://www.blog.duomly.com/what-is-the-difference-between-shadow-dom-and-virtual-dom/
DOM (Document Object Model) is a fundamental concept in front-end, and for sure, everyone who tried to learn programming has heard about it more than once. For beginners, it’s not so easy to understand what it exactly is and how to manipulate it. DOM manipulation isn’t so easy and comfortable, and the most important, it brings a lot of issues with performance. Nowadays, there are two essential concepts of DOM came with progressive web frameworks like Angular, React.js or Vue.js, Shadow DOM and Virtual DOM. In this article, I want to explain:
I will also try to tell you about the differences between them and what advantages and disadvantages each of them brings. Let’s start with an explanation of what DOM is.
Beginners often mislead DOM with what they create as an HTML structure of their website or application. But HTML becomes the DOM after the browser parses it, then it becomes the DOM. The definition says that DOM is an API for HTML or XML documents and it creates a logical structure that can be accessed and manipulated.
By DOM modification, we can understand adding, delete, or modify the elements of the website, assigning them different behavior, etc.
Every browser has its global object, called a window. Inside the window, there are different properties and methods. One of the properties in the window object is a document where we may find lots of properties and methods which can be used to access the DOM elements to interact with them. The graphic representation of the DOM looks like in the example below:
As you can see, it’s created as a tree. It starts from the main document object, then the html object is created, and html element leads to head and body, and so on. Each of the objects represents an HTML element from the website as an object, with properties, attributes, and methods which allows to modify it.
Shadow DOM is a tool used to build component-based apps and websites. Shadow DOM comes in small pieces, and it doesn’t represent the whole Document Object Model. We can see it as a subtree or as a separate DOM for an element. Shadow DOM can be imaged like bricks from which the DOM is created.
The main difference between DOM and Shadow DOM is how it’s created and how it behaves. Normally DOM nodes which we create are placed inside other elements, like in the tree we saw before.
In the case of Shadow DOM, we create a scoped tree, which is connected to the element but separated from the children elements. It’s called shadow tree and the element it’s attached to is called shadow host. And here we come to a great advantage of the Shadow DOM, everything which we will add to Shadow DOM is local, even styles.
Let’s explain why Shadow DOM is so useful and what issues it solves. First of all, it isolates the DOM, so the DOM of the component is a separate element which won’t appear in a global DOM. Another issue it helps with is scoping of the CSS, which means styles created inside the single Shadow DOM element are isolated and stays in the scope of that Shadow DOM. It simplifies styling a lot as we don’t have to worry a lot about naming space and we can use simple selectors and class names.
Also, we can think of the application as it is built from chunks (it is based on the components actually) and not as a one massive, global object. Shadow DOM can affect the performance of the application. As said at the beginning of the article, there are a lot of performance issues while we want to manipulate the DOM, because every change will make a re-rendering of the whole object. In the case of Shadow DOM browser knows which part should be updated.
Virtual DOM is a concept of DOM used by React.js and Vue.js.
In Virtual DOM concept copy of DOM is saved in the memory and while any change is done in the DOM, it’s compared to find differences. Then browser knows which elements were changed and can update only those part of the application to avoid re-rendering all the DOM. It’s done to improve the performance of the UI libraries.
As we know, from the previous paragraph in DOM, every element is re-rendered, no matter if it was changed or not. Let’s check in depth how Virtual DOM works step by step. So first, the change is done, and it’s done to the Virtual DOM, not to the original DOM, then the Virtual DOM is compared with the Document Object Model, and this process is called „diffing”.
While the differences are found then browser know which elements in the original DOM should be updated and the update is done. In the Virtual DOM concept, it’s possible to apply more than one change at once, to avoid re-rendering for every single element change. The biggest issue that Virtual DOM solves is the performance improvement on DOM manipulation.
The only thing which is common for both is that they help with performance issues. Both create a separate instance of the Document Object Model; besides this, both concepts are different.
Virtual DOM is creating a copy of the whole DOM object, and Shadow DOM creates small pieces of the DOM object which has their own, isolated scope for the element they represent.
DOM concept is very important in front-end programming, but with the development of technology and new libraries, improvements came to Document Object Model as well. With progressive web frameworks, we can use Shadow DOM and Virtual DOM to avoid issues with performance and modify the DOM faster and more efficient.
Now, it’s not a point of worry anymore, how to interact with the DOM object to not charm the performance, as the growth of technology came with help.
I hope you find this article helpful in understanding what the DOM is and how Virtual and Shadow DOM concepts work. Also, I explained the main differences between them and the issues they solved.
Have a nice coding!
Anna from Duomly
In this lesson, we learn about the Document Object Model. We learn what the DOM, Shadow DOM, and Virtual DOM are and how they work. We also look at some of the practical examples to understand them better.
#dom #shadow dom #virtual dom
Node js, express js, and MongoDB complete tutorials playlist:
Follow Tutorials website:
ReactJS Tutorials in Hindi | Introduction | Part-1
Node.js Tutorials for beginners in hindi | Setup Environment to run node js code | Part-2
ReactJS Tutorials in Hindi | Environment Setup using NPM or NPX | Part-3
#dom #virtual dom #reactjs #reactjsforbeginners
Mobiweb Technologies offers extensive Virtual Sports Betting Software solutions that includes a variety of sports like Football, Horse racing, Golf, Tennis etc. on which bettors can place wager. Team focuses on realistic animation creation that enhances the user-experience. This kind of special feature attracts fans from different regions and brings a large amount of betting options. With a dedicated team of sports betting software developers, it is possible for us to design customized and engaging betting softwares, the only difference from real-betting is that users can bet online at any time by just one click.
#virtual sports betting software #virtual sports betting app development #virtual sports betting app developers #virtual sports betting software developers #virtual sports betting website developers
Conversational AI has seen an exponential rise in popularity in the last decade and has become mainstream over the past two years. Enterprise adoption of Conversational AI is accelerating. Businesses are investing millions of dollars for conversational AI applications with massive expectations to improve customer experience and operational efficiencies.
Gartner_ says – “By 2022, 70% of white-collar workers will interact with conversational platforms on a daily basis.”_
Chatbots and Virtual Assistants are leveraging AI technologies to provide innovative and efficient solutions benefiting customers, employees, and partners. In this scenario, it is crucial to understand the fundamental differences between a Chatbot and a more sophisticated solution like a Virtual assistant.
Businesses and professionals who are planning or implementing a conversational AI solution should know this difference to reap benefits from these technologies. Without such proper understanding, there will be a high chance of failure to meet the expectations.
#chatbots-vs-virtual-assistants #chatbots #virtual-assistant #intellugent-virtual-assistant #chatbot-and-virtual-assistant #ai-chatbot #chatbot-development #use-cases-of-chatbots
AppClues Infotech is one of the leading mobile app development company in USA that develops secured on-demand virtual event streaming applications for multiple platforms (Android & iOS) and various Industries. We have a dedicated team of designers and developers that are highly proficient to make any type of custom mobile application as per your specific needs.
If you have any ideas for virtual event streaming app development kindly share them with us to get the right solution for your dream project.
#top virtual event streaming app development services in usa #how to build virtual event application #cost and features in virtual events app development #how much does it cost to make a virtual live streaming app #virtual event streaming app development #online event app development