JavaScript event loop case study

JavaScript event loop case study

Event loop in js

Because JavaScript is single-threaded, the same event can only execute one method, so the methods in the program will be added to the execution stack and executed in order of last-in-first-out. When encountering an asynchronous task, it will not be blocked, but the task will be placed in the event queue and the synchronous code in the execution stack will continue to execute. When all tasks in the current execution stack are executed, the task in the event queue will be searched, and the task's callback function will be placed in the execution stack to execute the synchronous code in it. This repeated cycle is called an event loop.

node.js

Features of Node.js

Event-driven

Execute the code from top to bottom, and when a callback is required, add it to the event queue. When the main thread finishes running, execute the callback in the event queue. The whole process will not block new events, nor does it need to maintain already established events.

Non-blocking io

When the main thread is idle, it starts to loop the event queue and process the events in the event queue. If the event is not an IO task, it will be processed by itself. If it is an IO task, it will be handed over to the thread pool for processing and a callback function will be specified. Then, other events in the loop queue will continue. When the blocking operation is completed, the result and the callback function will be placed in the queue, and the callback function will be executed when the main thread loops.

Node.js advantages and disadvantages

advantage

  1. High concurrency: Node.js uses a main thread to handle all requests, and then handles IO operations asynchronously, avoiding the overhead and complexity of creating and destroying threads and switching between threads.
  2. Suitable for io-intensive applications

shortcoming:

  1. Not suitable for CPU-intensive applications: long-term calculations will cause the CPU time slice to not be released, making it impossible to initiate subsequent IO events
  2. Cannot fully utilize multi-core CPUs
  3. Low reliability. Once a certain part of the code crashes, the entire system will crash.

Applicable scenarios:

  1. RESTful API: Requests and responses require only a small amount of text and do not require a lot of logical processing. Can handle tens of thousands of connections, just request the API to organize the data and return it
  2. In scenarios with a large number of Ajax requests
  3. Chat service: lightweight, high traffic, no complex computing logic

Node.js event loop

The node event loop relies on the libuv engine. After v8 interprets the js code, it calls the corresponding node api. These apis are driven by the libuv engine to perform corresponding tasks and put different events into different queues waiting for the main thread to execute. Therefore, the node event loop exists in the libuv engine.

libuv engine: implements event loop, file operations, etc., and is the core of node.js to achieve asynchronous

The single thread of node.js only means that JavaScript runs in a single thread, and io operations can be completed through the thread pool internally

poll (query phase) ---》check (check phase) ---》close callback (close event callback phase) ---》timer (timer detection phase) ---》io callback phase ---》idle phase ---》polling phase

Poll phase (polling phase):

V8 parses the js code and passes it to the libuv engine, and the loop first enters the poll stage. First check whether there is an event in the poll queue, and if there is, execute the callback in first-in-first-out order

This is the end of this article about the JavaScript event loop case study. For more information about JavaScript event loop, 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:
  • Learn more about the most commonly used JavaScript events
  • Analysis of JavaScript's event loop mechanism
  • JavaScript event capture bubbling and capture details
  • Detailed explanation of js event delegation
  • A brief analysis of event bubbling and event capture in js
  • Detailed explanation of Javascript event capture and bubbling methods

<<:  Detailed steps to install xml extension in php under linux

>>:  Basic usage and examples of yum (recommended)

Recommend

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can b...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Convert psd cut image to div+css format

PSD to div css web page cutting example Step 1: F...

Summary of the use of html meta tags (recommended)

Meta tag function The META tag is a key tag in th...

Detailed explanation of screen command usage in Linux

GUN Screen: Official website: http://www.gnu.org/...

CSS3 to achieve dynamic background gradient effect

Learning CSS3 is more about getting familiar with...

Win10 + Ubuntu20.04 LTS dual system boot interface beautification

Effect display The built-in boot interface is too...

Detailed explanation of the role of the new operator in Js

Preface Js is the most commonly used code manipul...

How to achieve seamless token refresh

Table of contents 1. Demand Method 1 Method 2 Met...

VMware, nmap, burpsuite installation tutorial

Table of contents VMware BurpSuite 1. Virtual mac...

MySQL 8.0.20 installation and configuration detailed tutorial

This article shares with you a detailed tutorial ...

How to use css variables in JS

How to use css variables in JS Use the :export ke...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to ...