Front-end JavaScript operation principle

Front-end JavaScript operation principle

1. What is a JavaScript engine?

JavaScript engine is a computer program whose main function is to compile source code into machine code when JavaScript is running.

Each major web browser has its own JavaScript engine, which is usually developed by the web browser vendor.

  • Google Chrome V8。
  • Mozilla Firefox Spider Monkey。
  • Safari Javascript Core Webkit。
  • Edge (Internet Explorer)

Previous JavaScript engines were mainly used in web browsers, but with the emergence of nodejs , this limitation was broken.

2. V8 engine

V8 includes parser , an interpreter ( Ignition ), and an optimizing compiler ( TurboFan ).

Parser: used to generate an abstract syntax tree.

Interpreter (Ignition): Converts source code into bytecode.

Optimizing compiler ( TurboFan ): performs some optimizing compilation optimizations, such as inline caching.

The following is the general workflow of the V8 engine.

  • First, the parser generates an abstract syntax tree.
  • The interpreter then generates V8 format bytecode based on the syntax tree.
  • The optimizing compiler then compiles the bytecode into machine code.

3. Runtime Environment

In the browser running environment, the browser provides Web API , such as HTTP requests, timers, events, etc.

In the server running environment, nodejs provides an API.

Below is the architecture of JavaScript when it is running in a browser. It includes a memory heap, a memory stack, an event loop, and a callback queue.

stack
heap
call stack
callback queue
event loop

4. Runtime call stack

The following code shows the call stack changes of JavaScript execution.

function add(x, y) {
    return x + y;
}

function print(x, y) {
    console.log('x+y=',add(x, y))
}

print(1, 3)

5. Asynchronous tasks

JavaScript first executes the print function and then calls Web API setTimeout() . Web API stores the callback function of setTimeout() and adds the callback function to the callback queue after 3 seconds. The event loop finds that the call stack is empty, so it moves the callback function in the queue to the call stack for execution.

function add(x, y) {
    return x + y;
}

function print(x, y) {
    setTimeout(function (){
        console.log('x+y=',add(x, y))
    }, 3000)
}

print(1, 3)

6. Summary

JavaScript runs mainly on the JavaScript engine and runtime environment. The engine translates the js source code into machine code understood by the computer, and the runtime environment provides some APIs and runtime implementations for communicating with the underlying computer.

This is the end of this article about the operating principles of front-end JavaScript. For more relevant content on the operating principles of JavaScript, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Understanding the execution order of javascript operation mechanism
  • Example Analysis of JavaScript Operation Mechanism
  • Analysis of JavaScript operation principle
  • Efficiently run code analysis in JavaScript
  • Do you know the three steps of JavaScript js running?

<<:  How to enable remote access in Docker

>>:  HTML Tutorial: Collection of commonly used HTML tags (4)

Recommend

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

js object to achieve data paging effect

This article example shares the specific code of ...

Comparison of two implementation methods of Vue drop-down list

Two implementations of Vue drop-down list The fir...

The role and opening of MySQL slow query log

Preface The MySQL slow query log is a type of log...

How to use Vuex's auxiliary functions

Table of contents mapState mapGetters mapMutation...

CentOS6 upgrade glibc operation steps

Table of contents background Compile glibc 2.14 M...

Tutorial on installing GreasyFork js script on mobile phone

Table of contents Preface 1. Iceraven Browser (Fi...

Summary of discussion on nginx cookie validity period

Every visit will generate Cookie in the browser, ...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...

Process parsing of reserved word instructions in Dockerfile

Table of contents 1. What is Dockerfile? 2. Analy...

MYSQL's 10 classic optimization cases and scenarios

Table of contents 1. General steps for SQL optimi...