search
HomeWeb Front-endFront-end Q&AWhat support does javascript rely on?

What support does javascript rely on?

Sep 30, 2022 pm 03:19 PM
javascript

Javascript relies on the support of the underlying javascript engine. JavaScript runs in the browser and mainly relies on the browser's JS engine to interpret and execute JS code; the JavaScript engine is a virtual machine that specializes in processing JavaScript scripts and is generally included with web browsers for interpreting and executing JS scripts.

What support does javascript rely on?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript relies on the support of the underlying js engine.

Javascript runs in the browser and mainly relies on the browser's js engine to interpret and execute js code. Other software with a js engine can also run js, but generally js is closely related to web pages, so it is generally run in the browser.

javascript engine

The JavaScript engine is a virtual machine that specializes in processing JavaScript scripts. It is generally included with web browsers and is used to Interpret and execute js scripts.

Famous js engine:

Mozilla: SpiderMonkey engine, the world's first JavaScript engine, written in C/C, used in Mozilla Firefox versions 1.0~3.0

Google: V8 engine, written in C/assembly language, used in the chrome browser

Microsoft: Chakra (Chakra, laugh) engine, used in the 32-bit version of Internet Explorer 9

The relationship between browser kernel and JS engine

Take webkit as an example:

What support does javascript rely on?

V8 engine

#1. Principle of V8 engine

V8 engine is Google’s open source high-performance JavaScript and WebAssembly engine written in C. For Chrome and Node.js, etc.

It implements ECMAScript and WebAssembly and runs on Windows 7 or higher, macOS 10.12 and Linux systems using x64, IA-32, ARM or MIPS processors.

The V8 engine can run independently or be embedded into any C application. For example, the use of Node.js in the V8 engine can be regarded as embedding the V8 engine into the application. Then Node.js has the ability to execute JavaScript code.

Principle diagram:

What support does javascript rely on?

①. The Parse module will convert JavaScript code into AST. This is because the interpreter does not directly understand JavaScript code. If the function is not called, it will not be converted to AST

②. Ignition is an interpreter and will convert AST to ByteCode. At the same time, the information required for TurboFan optimization will be collected (such as the type information of function parameters, only with types can real operations be performed). If the function is called only once, Ignition converts the AST into ByteCode

③. Why is it finally converted into bytecode instead of directly into machine code?

Because the environment in which JS code is executed is not fixed. It may be using a browser in a Windows environment, a mac environment, or a Linux environment, or it may be in Node.js. The environment is not fixed, and there will be different CPUs in different environments. Different CPUs have different CPU architectures, and different architectures can execute different machine instructions.

What support does javascript rely on?

is converted into bytecode specified by the V8 engine. It can be executed in any environment and is cross-platform. Finally, the V8 engine will convert the bytecode into assembly. Instructions are then converted into CPU instructions corresponding to different environments.

But it’s still not convenient enough to go through this process every time. For example, there is a function that is reused, but using the previous set of processes, every time this function is used, it needs to be converted into bytecode and then turned into CPU instructions. The performance is relatively low. If this function can be directly Convert it into machine instructions and save them. When using this function, run the machine instructions directly, which has higher performance. However, if this function is only run once, there is no need to convert it into machine code and save it, which will waste space.

④. Use the TurboFan library, which is a compiler that compiles bytecode into machine code that can be directly executed by the CPU. It can use ignition to collect function execution information and learn which functions have comparative execution times. Many will mark such functions as hot, hot functions, and then convert this function into optimized machine instructions. When using this hot function in the future, there is no need to go through the above tedious process and just execute the machine instructions directly.

But in fact the machine code will also be restored to ByteCode. This is because if the type changes during the subsequent execution of the function, the previously optimized machine code cannot handle the operation correctly and will be converted in reverse. is bytecode.

⑤. Deoptimization: For example, there is a function

function  sum(num1,num2){
   num1+num2
}

that calls the sum function

sum(20,30)
sum(28,30)

如果传入数字,调用sum函数,需要做的工作就是对两个数字进行相加,执行的机器指令永远是对这两个数字进行相加.
一旦改变传入值的类型,如果变成字符串,那么这个函数的意思就是两个字符串拼接。

sum("aaa","bbb")

这两种类型的传入值执行“+”操作对应的机器指令是不同的,JavaScript是不会对传入值的类型做检测的,那么还是使用数字相加的机器指令,这次函数调用的结果是不能够使用的。

但是V8引擎中提供了一种解决办法Deoptimization过程,这个过程是,一旦发现在执行机器指令时候,执行的操作不一样的时候,Deoptimization会反向优化,又转化为字节码,执行后续操作。

2、V8引擎的解析图

What support does javascript rely on?

V8执行的细节:

①、Blink将源码交给V8引擎,Stream获取到源码并且进行编码转换

②、scanner会进行词法分析,词法分析之后会将代码转换为成tokens

③、tokens会被转换为AST树,经过Parser和PreParser:

Parser就是直接将tokens转换为AST树架构;

PreParser预解析,为什么会需要预解析?

1)如上图中的函数outer(),内部有一个函数inner(),但是并没有任何调用inner()的代码,那么就意味着并不是所有的JavaScript代码,都是一开始就被执行。对所有的JavaScript代码进行解析,必定会影响网页的运行效率。

2)V8引擎实现了Lazy Parsing(延迟解析)的方案,作用是将不必要的函数进行预解析,我只需要知道有这么个函数就行,也就是只解析暂时需要的内容,对函数的全量解析在函数被调用的时候才会执行。

3)例如上图中函数outer中的inner函数,它就是会执行预解析。

④、生成AST树之后,会被Ignition转成字节码,之后的过程就是代码的执行过程。

【相关推荐:javascript视频教程编程视频

The above is the detailed content of What support does javascript rely on?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
React: The Foundation for Modern Frontend DevelopmentReact: The Foundation for Modern Frontend DevelopmentApr 19, 2025 am 12:23 AM

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

The Future of React: Trends and Innovations in Web DevelopmentThe Future of React: Trends and Innovations in Web DevelopmentApr 19, 2025 am 12:22 AM

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.

React: A Powerful Tool for Building UI ComponentsReact: A Powerful Tool for Building UI ComponentsApr 19, 2025 am 12:22 AM

React is a JavaScript library for building user interfaces. Its core idea is to build UI through componentization. 1. Components are the basic unit of React, encapsulating UI logic and styles. 2. Virtual DOM and state management are the key to component work, and state is updated through setState. 3. The life cycle includes three stages: mount, update and uninstall. The performance can be optimized using reasonably. 4. Use useState and ContextAPI to manage state, improve component reusability and global state management. 5. Common errors include improper status updates and performance issues, which can be debugged through ReactDevTools. 6. Performance optimization suggestions include using memo, avoiding unnecessary re-rendering, and using us

Using React with HTML: Rendering Components and DataUsing React with HTML: Rendering Components and DataApr 19, 2025 am 12:19 AM

Using HTML to render components and data in React can be achieved through the following steps: Using JSX syntax: React uses JSX syntax to embed HTML structures into JavaScript code, and operates the DOM after compilation. Components are combined with HTML: React components pass data through props and dynamically generate HTML content, such as. Data flow management: React's data flow is one-way, passed from the parent component to the child component, ensuring that the data flow is controllable, such as App components passing name to Greeting. Basic usage example: Use map function to render a list, you need to add a key attribute, such as rendering a fruit list. Advanced usage example: Use the useState hook to manage state and implement dynamics

React's Purpose: Building Single-Page Applications (SPAs)React's Purpose: Building Single-Page Applications (SPAs)Apr 19, 2025 am 12:06 AM

React is the preferred tool for building single-page applications (SPAs) because it provides efficient and flexible ways to build user interfaces. 1) Component development: Split complex UI into independent and reusable parts to improve maintainability and reusability. 2) Virtual DOM: Optimize rendering performance by comparing the differences between virtual DOM and actual DOM. 3) State management: manage data flow through state and attributes to ensure data consistency and predictability.

React: The Power of a JavaScript Library for Web DevelopmentReact: The Power of a JavaScript Library for Web DevelopmentApr 18, 2025 am 12:25 AM

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

React's Ecosystem: Libraries, Tools, and Best PracticesReact's Ecosystem: Libraries, Tools, and Best PracticesApr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

React and Frontend Development: A Comprehensive OverviewReact and Frontend Development: A Comprehensive OverviewApr 18, 2025 am 12:23 AM

React is a JavaScript library developed by Facebook for building user interfaces. 1. It adopts componentized and virtual DOM technology to improve the efficiency and performance of UI development. 2. The core concepts of React include componentization, state management (such as useState and useEffect) and the working principle of virtual DOM. 3. In practical applications, React supports from basic component rendering to advanced asynchronous data processing. 4. Common errors such as forgetting to add key attributes or incorrect status updates can be debugged through ReactDevTools and logs. 5. Performance optimization and best practices include using React.memo, code segmentation and keeping code readable and maintaining dependability

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools