Home  >  Article  >  Web Front-end  >  Is nodejs js?

Is nodejs js?

青灯夜游
青灯夜游Original
2021-11-01 15:34:163096browse

Nodejs is not js, but a platform that allows js to run on the server. js (full name JavaScript) is a programming language, and Node is a JavaScript running environment based on the Chrome V8 engine, a development platform that allows JavaScript to run on the server.

Is nodejs js?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Nodejs is not js, but a platform that allows js to run on the server.

Node.js was released in May 2009 and developed by Ryan Dahl. It is a JavaScript running environment based on the Chrome V8 engine. It uses an event-driven, non-blocking I/O model to allow JavaScript to run in A server-side development platform that makes JavaScript a scripting language on par with server-side languages ​​such as PHP, Python, Perl, and Ruby.

JavaScript can perform server-side programming based on Node.js technology.

Features of Node.js

Non-blocking asynchronous io

For example, when accessing When the database obtains data, it takes a while. In the traditional single-thread processing mechanism, after executing the code to access the database, the entire thread will pause and wait for the database to return the result before executing the subsequent code. In other words, I/O blocks the execution of the code, greatly reducing the execution efficiency of the program.

Due to the non-blocking I/O mechanism used in Node.js, after the code that accesses the database is executed, the code behind it will be executed immediately, and the processing code for the database return result will be placed in callback function, thus improving the execution efficiency of the program.

When an I/O is completed, the thread performing the I/O operation will be notified in the form of an event, and the thread will execute the callback function of this event. In order to handle asynchronous I/O, the thread must have an event loop, constantly checking whether there are unhandled events, and processing them in turn.

In blocking mode, one thread can only handle one task. If you want to improve throughput, you must use multi-threading. In non-blocking mode, a thread is always performing calculation operations, and the CPU core utilization of this thread is always 100%. Therefore, this is a particularly philosophical solution: Rather than having many people idle, it is better for one person to risk his life and work to death.

Single thread

In server-side languages ​​such as Java, PHP or .net, a new thread is created for each client connection. Each thread requires approximately 2MB of memory. In other words, theoretically, the maximum number of users that can be connected to a server with 8GB of memory at the same time is about 4,000. In order for a Web application to support more users, the number of servers needs to be increased, and the hardware cost of the Web application will of course increase.

Node.js does not create a new thread for each client connection, but only uses one thread. When a user connects, an internal event is triggered. Through non-blocking I/O and event-driven mechanisms, the Node.js program is macroscopically parallel. Using Node.js, a server with 8GB of memory can handle more than 40,000 user connections simultaneously.

In addition, the benefits brought by single threading are that the operating system no longer has the time overhead of thread creation and destruction. However, single threading also has many disadvantages, which will be explained in detail in the disadvantages of Node.js, please continue reading.

Event-driven

In Node, when the client requests to establish a connection, submit data and other behaviors, corresponding events will be triggered. In Node, only one event callback function can be executed at a time, but in the middle of executing an event callback function, other events occur, and other events can be processed instead (for example, a new user connects), and then Return the callback function that continues to execute the original event. This processing mechanism is called the "event loop" mechanism.

The bottom layer of Node.js is C (V8 is also written in C). Nearly half of the underlying code is used for the construction of event queues and callback function queues. Using event-driven to complete server task scheduling is something only a genius can think of. The dance on the tip of the needle, with one thread, takes on the mission of handling a lot of tasks.

Is nodejs js?

Pay attention to the event loop here, which can also be said to be the essence of Node.js. Here is a quote from the Node.js official website

  ┌───────────────────────────┐
┌─>│           timers          │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │     pending callbacks     │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │       idle, prepare       │
│  └─────────────┬─────────────┘      ┌───────────────┐
│  ┌─────────────┴─────────────┐      │   incoming:   │
│  │           poll            │<─────┤  connections, │
│  └─────────────┬─────────────┘      │   data, etc.  │
│  ┌─────────────┴─────────────┐      └───────────────┘
│  │           check           │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
└──┤      close callbacks      │
   └───────────────────────────┘

Quote A piece of content from the Node official website:

Note: Each box will be called a "phase" of the event loop.
Each stage has a callback FIFO queue to be executed. While each stage is special in its own way, generally when the event loop enters a given stage, it will perform whatever operations are specific to that stage and then execute callbacks on that stage's queue until the queue is exhausted. Exhausted or maximum number of callbacks have been executed. When the queue is exhausted or the callback limit is reached, the event loop moves to the next phase, and so on.
Regarding the event loop, it is a core point. Interviewers often ask questions about specific execution output. You can read my article

Cross-platform

Initially, Node can only run on the Linux platform. Later, with the development of Node, Microsoft noticed its existence and invested a team to help Node achieve compatibility with the Windows platform. When the v0.6.0 version was released, Node could already run directly on the Window platform. Node is cross-platform based on libuv.

Disadvantages of Node.js

Disadvantages caused by single thread

Node.js One of the features is single-threading, which brings many benefits, but it also has disadvantages. The weaknesses of single-threading are as follows.

  • Unable to utilize multi-core CPU

  • The error will cause the entire application to exit and cannot continue to call asynchronous I/O

  • A large amount of calculations occupy the CPU, making it impossible to continue calling asynchronous I/O

Application scenarios of Node.js

Introducing the characteristics and disadvantages of Node.js, and then talking about the application scenarios of Node.js.

What kind of applications is Node.js suitable for developing?

Good at I/O, not good at calculation. Because Node.js is best at task scheduling, if your business has a lot of CPU calculations, it actually means that this calculation blocks this single thread, which is not suitable for Node development, but it is not without solutions, just saying Not quite suitable.

Node.js is very suitable when the application needs to handle a large amount of concurrent I/O and does not need to perform very complex processing inside the application before sending a response to the client. Node.js is also very suitable for working with web sockets to develop real-time interactive applications with long connections.

The specific scenarios can be expressed as follows:

  • The first category: user form collection system, background management system, real-time interaction system, examination system, networking software, high-end Concurrency web applications;

  • The second category: multiplayer online games based on web, canvas and other;

  • The third category : Web-based multi-person real-time chat client, chat room, graphic live broadcast;

  • The fourth category: single-page browser application;

  • The fifth category: operating databases, providing json-based APIs for front-end and mobile terminals;

  • The sixth category,...

Which big companies are using it

  • Yahoo: Yahoo has opened the Cooktail framework, extending the capabilities of the YUI3 front-end framework to the server side with the help of Node.

  • Tencent: Apply Node to long connections to provide real-time functionality.

  • Huapet.com, Mogujie: Real-time notification through socket.io.

  • Alibaba: The main use is the performance of parallel I/O to achieve efficient distribution. They have also developed many Node frameworks

  • LinkedIn: Mobile websites also use Node

  • NetEase: The gaming field has high requirements for concurrency and real-time, and NetEase has open sourced Node’s real-time framework pomelo

  • Wait...

[Recommended learning: "nodejs tutorial"]

The above is the detailed content of Is nodejs js?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is javascript uberNext article:what is javascript uber