Home > Web Front-end > Front-end Q&A > What support does javascript rely on?

What support does javascript rely on?

青灯夜游
Release: 2022-09-30 15:19:33
Original
2316 people have browsed it

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
}
Copy after login

that calls the sum function

sum(20,30)
sum(28,30)
Copy after login

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

sum("aaa","bbb")
Copy after login

这两种类型的传入值执行“+”操作对应的机器指令是不同的,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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template