Before we can optimize our applications (using Memoization), we must understand what a Render actually is at the memory level.
We write JSX because it looks like HTML, and it allows us to visualize the layout. However, browsers cannot read JSX. React doesn't run JSX. React runs the result of the Babel transformation. When we write this:
const element = <h1 className="title">Hello</h1>;Babel turns it into a standard JavaScript function call:
// The Reality: What the browser actually runs
const element = React.createElement(
"h1", // type
{ className: "title" }, // props
"Hello" // children
);The result of React.createElement() is not a DOM node. It is a plain, lightweight JavaScript Object. We call this a React Element.
// The Result in Memory
const element = {
type: "h1",
props: {
className: "title",
children: "Hello",
},
key: null,
ref: null,
$$typeof: Symbol.for("react.element"), // Security tag against XSS
};There are two critical characteristics of these Blueprint objects:
className to active), we must create a brand new object. We cannot scribble on the old object.React is okay with creating and destroying 10,000 of these simple objects per second. Modern JavaScript engines (V8 in Chrome) are incredibly fast at handling short-lived objects. The performance cost of React usually comes from the calculation inside our components, not the object creation itself.
The Virtual DOM isn't a magical separate software installed in our browser. It is simply the collection of these Blueprint Objects kept in memory.
When we say React compares the Virtual DOM, we mean:
Diffing).Reconciliation).| Concept | Definition | The Reality |
|---|---|---|
| JSX | Syntactic sugar for React. | Becomes React.createElement(). |
| React Element | The return value of a component. | A plain JS Object (The Blueprint). |
| Immutability | Unable to be changed. | We must create a new object to update UI. |
| Virtual DOM | The tree of React Elements. | Just a data structure in memory. |
| Render Phase | Calling the component function. | Creating Blueprints (Pure JS). |
| Commit Phase | Updating the browser. | Touching the actual DOM (Expensive). |
If Function A runs twice:
{ name: "Box" }{ name: "Box" }Are these the same object?
No. Object 1 !== Object 2.
They look the same, but they are different objects in memory.
This is the fundamental reason why we need useMemo and useCallback — to force React to keep the object the same between runs.
<div />) and converts the JSX into React function calls (React.createElement('div')). The browser never sees JSX. It receives a bundle file containing thousands of React.createElement function calls.React.createElement(...).The creation of the object happens in the browser, in production, live on the user's device. Every time a component renders, the browser CPU executes those functions and creates those objects in RAM.
Standard JavaScript objects are mutable. We could technically modify a property on an object in the heap.
However, React forces them to be Immutable for two reasons:
Object.freeze() (The Enforcement):
In Development mode, React actually runs Object.freeze(element) on the object immediately after creating it.
If we try to write element.props.name = "Bob", the browser will throw a strict JavaScript error: Cannot assign to read only property.... React intentionally locks the object to prevent us from breaking things.OldObject === NewObject.oldObject.title = "New Title"), the reference stays the same (oldObject === oldObject is true).So to sum it up they are immutable because React often freezes them programmatically, and because mutating them would silently break the UI updates.