Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the Operation principle of JavaScript engine

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "Analysis of the Operation principle of JavaScript engine". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Note: this article is mainly based on the V8 engine used by Node.js and Chrome-based browsers.

The HTML parser encountered a script tag with source code. Code from this source is loaded from the network, cache, or installed service worker. The response is to use the requested script as a byte stream, which is the responsibility of the byte stream decoder. The byte stream decoder decodes the byte stream when it is downloaded.

The byte stream decoder creates tokens from the decoded byte stream. For example, 0066 is decoded to f, 0075 to upright 006e to n, 0063 to c, 0074 to t, 0069 to I, 006f to o, 006e to n, followed by a space. Just like function in JS, which is a reserved keyword in JS, it creates a tag and sends it to the parser. The same is true for the rest of the byte stream.

The engine uses two parsers: the pre-parser (pre-parser) and the parser (parser). The pre-parser only checks the tags in advance to see if there are any syntax errors. This reduces the time it takes to find errors in your code, otherwise the parser will find them later.

If there are no errors, the parser creates a node based on the tag received from the byte stream decoder. Using these nodes, it creates an abstract syntax tree, AST.

Next, it's the interpreter (interpreter). An interpreter that traverses the AST and generates bytecode based on the information contained in the AST. Once the bytecode is fully generated, the AST is deleted, thus clearing the memory space. Finally, the generated machine code can be run on the computer.

Although the bytecode is fast, it can be faster. When this bytecode runs, information is generated. It can detect whether certain behaviors occur frequently and the type of data used. Maybe you've called a function dozens of times: it's time to optimize it so that it runs faster!

The bytecode is sent to the optimization compiler (ptimizing compiler) along with the generated type feedback. The optimized compiler receives bytecode and type feedback and generates highly optimized machine code based on this information.

JS is a dynamically typed language, which means that data types can be constantly changed. If the JS engine checks the data type of a value every time, it will be very slow.

Instead, the JS engine uses a technique called inline caching (inline caching). It caches the code in memory in the hope that it will return the same value in the same behavior in the future. Suppose a function is called 100 times and has always returned the same value so far. It will assume that this value will be returned when it is called for the 101st time.

Suppose we have the following function sum, which (so far) has been called each time with a numeric value as an argument:

Ffunction sum (a, b) {return a + b} sum (1,2)

The execution result is 3. The next time it is called, it will assume that we call it again with two identical numbers.

If you fake it, you don't need to find it dynamically, just use the results stored in a specific memory slot, which already has a reference. Otherwise, if the assumption is incorrect, it will reverse optimize the code and revert to the original bytecode instead of the optimized machine code.

For example, the next time we call it, we pass a string instead of a number. Because JS is dynamically typed, there is nothing wrong with doing so.

This is the end of the introduction of function sum (a, b) {return a + b} sum ('1mm, 2) "Analysis of the Operation principle of JavaScript engine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report