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

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Use neat HTML markup to build your pages

The Internet is an organism that is constantly ev...

Implementing custom scroll bar with native js

This article example shares the specific code of ...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...

MySQL installation diagram summary

MySQL 5.5 installation and configuration method g...

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...

Analysis of the principle of Mybatis mapper dynamic proxy

Preface Before we start explaining the principle ...

Perfect solution for vertical centering of form elements

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

React sample code to implement automatic browser refresh

Table of contents What is front-end routing? How ...

Detailed explanation of JavaScript prototype and examples

Table of contents The relationship between the co...

HTML input file control limits the type of uploaded files

Add an input file HTML control to the web page: &...

In-depth explanation of JavaScript this keyword

Table of contents 1. Introduction 2. Understand t...

Detailed explanation of browser negotiation cache process based on nginx

This article mainly introduces the detailed proce...

Thumbnail hover effect implemented with CSS3

Achieve resultsImplementation Code html <heade...