DEV Community

Cover image for Real DOM vs Virtual DOM
_Khojiakbar_
_Khojiakbar_

Posted on

1

Real DOM vs Virtual DOM

1. Real DOM (Document Object Model)

  • What It Is: The Real DOM is the actual structure of the webpage, represented as a tree of objects. It is what the browser uses to render the HTML elements on the screen.

  • How It Works: When you update an element in the Real DOM (like changing text or adding a new element), the browser has to re-render that part of the page. This can be slow because the entire tree might need to be updated, even if only a small part changes.

  • Example: Imagine you’re in a library with a card catalog. Every time you change one card, you have to search through the whole catalog to update it. This takes time.

  • Pros:

    1. Direct manipulation of the UI.
    2. No additional abstraction layer.
  • Cons:

    1. Slow updates because the whole UI might need to be refreshed.
    2. High memory usage if the DOM is large.

2. Virtual DOM

  • What It Is: The Virtual DOM is a lightweight copy of the Real DOM. It’s a programming concept where a virtual representation of the UI is kept in memory and synced with the Real DOM using a library like React.

  • How It Works: When you update something in the Virtual DOM, it calculates the difference between the current state and the previous state (a process called "diffing"). Then, it only updates the parts of the Real DOM that actually changed.

  • Example: Imagine you have a digital card catalog on your computer. When you make a change, the computer quickly compares the old and new versions and only updates the specific card that changed. This is much faster.

  • Pros:
    1. Faster updates because only the changed parts of the DOM are
    re-rendered.

    2. Efficient use of memory and processing power.

  • Cons:
    1. Adds a layer of abstraction, which might be harder to debug.
    2. Initial setup can be more complex compared to directly
    manipulating the Real DOM.

Analogy

  • Real DOM: Like redecorating a room where you have to rearrange all the furniture whenever you buy a new chair.
  • Virtual DOM: Like using an app to visualize the room layout first, so you only move the furniture that actually needs to be moved when the new chair arrives.

The Virtual DOM helps improve performance by minimizing unnecessary updates to the Real DOM, making your applications run smoother and faster.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay