React, like any other JavaScript framework, handles a lot of things behind the scenes that we often don’t even think about.
And that’s okay—our job as developers is to solve problems, and the simpler the implementation, the better. You don’t always need to understand every detail of what a framework does for you.
JavaScript is an interesting language; it’s the king of the browser, and browsers are still used heavily, so I don’t see it disappearing anytime soon.
In fact, many native apps (iOS, Android, Smart TVs) run hybrid solutions using both native and web technologies.
In this post, I want to test a simple counter in React versus its Vanilla JavaScript version.
Measuring Performance with Chrome DevTools
First, let's talk about a useful tab that Chrome offers called Performance. This tab includes a recording feature that measures a web application's performance.
In this post, I'm going to focus on three key metrics: JS Heap, Nodes, and Listeners.
JS Heap: The heap is a region of memory in JavaScript where objects, arrays, and functions are stored. Unlike the stack, which holds primitive values (numbers, strings, booleans) and function calls, the heap manages dynamic memory allocation.
DOM Nodes: A DOM node is an individual element, attribute, or text within a web page's HTML, represented in the Document Object Model (DOM).
Event Listeners: In JavaScript, event listeners are functions that wait for specific events (e.g., clicks, key presses, mouse movements) on HTML elements. When the event occurs, the listener triggers, executing code in response.
Demo: Building a Basic React Counter
Alright, the UI is a simple counter. The UI is just a button with a click handler. Every time the button is clicked, the counter increments.
- React Demo
The React code looks like this:
"use client"; import { useState } from "react"; export default function Page() { const [counter, setCounter] = useState(0); const incrementClickHandler = (event: { preventDefault: () => void }) => { event.preventDefault(); setCounter((prevCounter) => prevCounter + 1); }; return ( <div style="{{" maxwidth: margin: auto> <a href="#" style="{{" display: padding: fontsize: border: solid black width: textalign: margintop: onclick="{incrementClickHandler}"> Increment {counter} </a> </div> ); }
The code is pretty self-explanatory. One thing to note is that the demo runs on top of Next.js, which is why we need "use client". Other than that, it's just a basic React component.
React Counter UI
20 Seconds and Only One Click
Now, I'm going to open the Performance tab in Chrome, click the record icon, and let it run for 20 seconds while clicking the button only once. At the end of the 20 seconds, the performance results look like this:
See how just one click bumps the numbers to:
React | |
---|---|
JS Heap | 3.4MB |
Nodes | 47 |
Listeners | 287 |
?
20 Seconds with a Click per Second
Now, I’m going to let it run for another 20 seconds, but this time I’ll click the button once per second. Let’s take a look at the results:
React | |
---|---|
JS Heap | 4MB |
Nodes | 46 |
Listeners | 331 |
Two things to note about React:
a) When a state variable is updated, the component is re-rendered, meaning that in this case, the component was rendered 20 times.
b) Thanks to the virtual DOM, React only updates the nodes that need to be updated.
Now, let’s go back to the chart and see how the blue line (JS Heap) and the yellow line (Listeners) increment, while the green line (Nodes) remains constant.
It's also worth mentioning that the numbers didn't change much compared to the one-click run.
Demo: Building a Vanilla JavaScript Counter
Now, we have the same UI, but this time it’s built with vanilla HTML and JavaScript—no frameworks involved.
- Vanilla Demo .
"use client"; import { useState } from "react"; export default function Page() { const [counter, setCounter] = useState(0); const incrementClickHandler = (event: { preventDefault: () => void }) => { event.preventDefault(); setCounter((prevCounter) => prevCounter + 1); }; return ( <div style="{{" maxwidth: margin: auto> <a href="#" style="{{" display: padding: fontsize: border: solid black width: textalign: margintop: onclick="{incrementClickHandler}"> Increment {counter} </a> </div> ); }
One thing to mention is the necessity of the following element:
<script> let increment = 0; window.onload = function () { document.querySelector("#counter").innerText = increment; document.querySelector("a").addEventListener("click", function (event) { event.preventDefault(); increment++; document.querySelector("#counter").innerText = increment; }); }; </script> <a href="#" style=" display: inline-block; padding: 20px 40px; font-size: 28px; border: 1px solid black; width: 100%; text-align: center; text-decoration: none; color: black; margin-top: 40; box-sizing: border-box; ">Increment <span id="counter"></span> </a>
that is manipulated with JavaScript to update its content:
<span id="counter"></span>
Vanilla Counter UI
20 Seconds and Only One Click
Again, I’m going to click the record icon and let it run for 20 seconds, clicking the button only once.
Take a look at the results:
Vanilla | |
---|---|
JS Heap | 1.7MB |
Nodes | 16 |
Listeners | 20 |
20 Seconds with a Click per Second
Again, I’m going to click the record icon and let it run for another 20 seconds, but this time, I’ll click the button once per second. Check out the results:
Vanilla | |
---|---|
JS Heap | 2.3MB |
Nodes | 42 |
Listeners | 77 |
Just like in the React example, the blue line (JS Heap) and the yellow line (Listeners) increased over time. However, the green line (Nodes) is not constant; it increases as the button is clicked.
A Few Words on Garbage Collection
Garbage Collection: The main concept that garbage collection algorithms rely on is the concept of reference.
JavaScript automatically handles garbage collection for us; we don’t need to trigger it manually. In the previous examples, we saw how resources are consumed, but at some point, JavaScript takes care of releasing some of those resources through its garbage collector.
Conclusion
One click or twenty clicks isn’t that different in terms of resource consumption. As soon as a click happens, JavaScript allocates resources, and subsequent clicks continue to consume resources. However, the jump isn't as significant as the transition from zero to one click.
Let’s take a look at the end values for 20 clicks in both versions:
Vanilla | React | |
---|---|---|
JS Heap | 2.3MB | 4.0MB |
Nodes | 42 | 46 |
Listeners | 77 | 331 |
It makes sense that React consumes more resources; that’s the cost of using a framework.
One key difference is that React attaches all the nodes from the beginning, while the vanilla version adds nodes as the clicks happen. However, in the end, both versions ended up with pretty much the same number of nodes.
The demo is quite simple, and at this level, there’s no significant difference in terms of performance. As mentioned earlier, there’s a price to pay for using the framework, but it’s worth it considering all the features and conveniences it provides.
The above is the detailed content of How Many Resources Does a Click Consume? React vs. Vanilla. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor