How to use VSCode to debug JS code on the browser
How to use VSCode to debug JS code on the browser? The following article will introduce to you how to use VSCode to debug the JS code of a web page. I hope it will be helpful to you!
Compared to looking at the code purely, I recommend looking at it in conjunction with the debugger, which allows us to see the actual execution route of the code and the changes in each variable. You can jump through large sections of code, or you can execute a certain piece of logic step by step. [Recommended learning: "vscode tutorial"]
Javascript code mainly has two running environments, one is Node.js and the other is the browser. Generally speaking, I will use VSCode’s debugger to debug JS code running on Node.js, and I will use chrome devtools to debug JS code running on the browser. Until one day I discovered that VSCode can also debug JS code on the browser. I tried it and it was really good.
How fragrant is it? Let's take a look together.
There is a .vscode/launch.json file in the root directory of the project, which saves the debugging configuration of VSCode.
We click the Add Configuration button to add a debugging chrome configuration.
The configuration is like this:
url is the address of the web page, we can run the local dev server , and then fill in the address here.
Then click debug to run:
VSCode will start a Chrome browser to load the web page and stop at our breakpoint. The call stack, scope variables, etc. will be displayed on the left panel.
The lowest level is of course the entrance to webpack. We can single-step debug the runtime part of webpack.
You can also take a look at the process from render, such as ReactDOM.render to rendering to a subcomponent, and what is done in the middle.
Or look at how the value of hooks of a component changes (the values of hooks are stored in the memerizedState attribute of fiberNode of the component):
As you can see, it is very convenient to debug webpack runtime code, or debug React source code, or business code.
Maybe you will say, this is also possible in chrome devtools. Is there anything special about it?
Indeed, chrome devtools can also do the same thing, but VSCode has two main benefits for debugging web page code:
Type the code in the editor Breakpoints allow you to change the code while debugging.
Use the same tools to debug Node.js code and debug web page code. Experience can be reused and the experience is consistent.
Regarding the first point, the sources of chrome devtools can actually modify the code and save it, but after all, it is not a specialized editor, so it is awkward to use it to write code. I personally am more accustomed to debugging and modifying code at the same time, and VSCode wins in this regard.
We usually use VSCode to debug Node.js, but you can also use VSCode to debug web pages. So as long as you are familiar with a tool, you don’t have to learn how to use chrome devtools, and the debugging experience is better with VSCode. , after all, it is the editor we use every day, and it is more convenient, which is why VSCode wins.
But you may say, what if I want to see profile information? That is, the time consumption of each function, which is important for analyzing code performance.
VSCode debugger also supports this:
Click the button on the left to record time-consuming information for a period of time, and you can manually stop, You can specify a fixed time or a certain breakpoint, which are three ways to select the execution process of a certain piece of code to record profile information.
It will save a xxx.cpuprofile file in the project root directory, which records the time it takes to execute each function. It can analyze the time consuming of a certain piece of code layer by layer to locate problems and optimize performance.
If you install the VSCode extension of vscode-js-profile-flame, you can also switch to flame graph display.
Some students may not understand flame diagrams, let me explain:
We know that the execution path of a certain function has a call stack. We can see which function is called step by step, which is a line.
But in fact, there is not just one function called by this function, it may be multiple:
The call stack is just saved It represents a route of execution to a certain function, while the flame graph saves all execution routes.
So you will see such a fork in the flame graph:
In fact, this is the execution process:
Let’s calculate a question:
Function A takes a total of 50 ms, the function B it calls takes 10 ms, and the function C it calls takes 20 ms. Question: Function A How many ms does the rest of the logic take?
Obviously it can be calculated as 50 - 10 - 20= 20 ms. Maybe you think function D takes too long, then take a look at the specific code and then see See if it can be optimized, and then check the time consumption.
It's that simple. This is how profile performance analysis is done, simple addition and subtraction.
The width of each box in the flame graph also reflects the time taken, so it is more intuitive.
The JS engine uses event loop to continuously execute JS code. Because the flame graph reflects the execution time of all codes, you will see how much time each event loop code execution takes.
The width of each strip represents the time consumption of each loop. Of course, the thinner the better, so that it will not block rendering. Therefore, the performance optimization goal is to turn the flame graph into small thin bars, not thicker.
Going back to the topic, compared to the performance of chrome devtools, VSCode’s cpu profile and flame graph are actually simpler and easier to use, and can meet most needs.
I think, unless you want to see rendering and memory information, because VSCode does not support it, you need to use chrome devtools. For debugging JS code, looking at profile information and flame graphs, VSCode is enough.
Anyway, I think VSCode is pretty cool for debugging JS code on web pages, what do you think?
Original address: https://juejin.cn/post/7010768454458277924
Author: zxg_God said there must be light
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of How to use VSCode to debug JS code on the browser. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to define header files using Visual Studio Code? Create a header file and declare symbols in the header file using the .h or .hpp suffix name (such as classes, functions, variables) Compile the program using the #include directive to include the header file in the source file. The header file will be included and the declared symbols are available.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to solve the problem that Chinese comments in Visual Studio Code become question marks: Check the file encoding and make sure it is "UTF-8 without BOM". Change the font to a font that supports Chinese characters, such as "Song Style" or "Microsoft Yahei". Reinstall the font. Enable Unicode support. Upgrade VSCode, restart the computer, and recreate the source file.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Common commands for VS Code terminals include: Clear the terminal screen (clear), list the current directory file (ls), change the current working directory (cd), print the current working directory path (pwd), create a new directory (mkdir), delete empty directory (rmdir), create a new file (touch) delete a file or directory (rm), copy a file or directory (cp), move or rename a file or directory (mv) display file content (cat) view file content and scroll (less) view file content only scroll down (more) display the first few lines of the file (head)

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
