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

How to configure eureka in docker

eureka: 1. Build a JDK image Start the eureka con...

Docker practice: Python application containerization

1. Introduction Containers use a sandbox mechanis...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

Javascript tree menu (11 items)

1. dhtmlxTree dHTMLxTree is a feature-rich Tree M...

How to transfer files between Docker container and local machine

To transfer files between the host and the contai...

A brief talk about MySQL semi-synchronous replication

Introduction MySQL achieves high availability of ...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...

MySQL8.0.18 configuration of multiple masters and one slave

Table of contents 1. Realistic Background 2. Agre...

Perfect solution for vertical centering of form elements

Copy code The code is as follows: <!DOCTYPE ht...

Introduction to CSS BEM Naming Standard (Recommended)

1 What is BEM Naming Standard Bem is the abbrevia...

WeChat Mini Programs Achieve Seamless Scrolling

This article example shares the specific code for...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

How to get/calculate the offset of a page element using JavaScript

question By clicking a control, a floating layer ...