当前位置:网站首页>Why is nodejs so fast?
Why is nodejs so fast?
2022-06-24 18:46:00 【Code Taoist】

If you are right about Node.js Shocked by the popularity of , So you're not alone . Don't worry , You're not the only one who feels this way .Node.js since 2009 Since its birth in, it has swept the field of online and application development , Become the most popular development environment .
Node.js There are many reasons why . For beginners , It has a large active community and a simple learning curve . This is probably PayPal、LinkedIn and Uber And other major brands use this framework and praise it for its excellent results .
In this paper , We will introduce Node.js And JavaScript The rest of the framework is different for each feature .
What makes Node.js So fast ?
When we talk about Node.js when , The first thing that comes to mind is the execution speed . therefore , In this section , We will try to figure out why Node.js Faster than the others . First , understand “ Execution speed ” The meaning of .
Execution speed can mean anything from computing Fibonacci numbers to searching databases .
Web Execution speed in the context of a service is all that is required to process requests and respond to clients . It is the time it takes to process the request , Start with the first connection , End when the client receives the response .
Let's take a look Node.js Several reasons why it is so fast :
- Node.js Is single threaded and asynchronous . That means all I/O Activities do not interfere with other processes . You can also send emails at the same time 、 Read the document 、 Query the database and so on .
- Yes Web Every request from the server will not have a different Node.js process . however , One Node.js The process will always run , Listening to the connection . all JavaScript The code runs in the main thread of the process , And all the others I/O Activities run in different threads , There is little delay .
- Responsible for the execution of JavaScript Of Node.js (V8) Virtual machine usage in JIT compile . At run time , Virtual machines can get source code and compile it into machine code . This means that all commonly used “ heat ” Functions can be compiled into machine code , So as to speed up the execution .
These views give us a deep understanding of Node.js Why so fast . Let's see what makes Node.js Completed all this .
Learn about asynchrony
With examples , We will try to master the concept of asynchronous processing .
Suppose you are on the top of the mountain , There is 100 A ball . Your goal is to send all the balls to the foot of the mountain in the shortest time . You obviously can't press all the balls at once , So you have to press one at a time . At this point you have two choices :
In the first way , You can push a ball , Then wait for it to reach the bottom , Then move on to the next . however , This method takes a long time to push all the balls to the bottom .
In the second way , You can push the ball one by one , Without waiting for them to reach the bottom . Using this method , You can push all... In the shortest possible time 100 A ball .
The first method is synchronous execution , The second method in this case is asynchronous execution . The previous example demonstrates that asynchronous execution is much faster than synchronous operation .
Now you have mastered the basics of asynchronous programming , Let's see how it helps Web Server performance .
Suppose each ball corresponds to a database query . therefore , If you have a lot of aggregations for one 、 Large projects such as search process data synchronously , Code execution will be blocked . however , If you are processing it asynchronously , You can run all the queries at once , And then collect the results .
On the back end ,Node.js And Angular、React、React Native and Vue.js Combination , Provide lightning like processing and rendering .
The event loop
An event loop is a mechanism responsible for scheduling events in a program that always runs asynchronously with the message originator .
When using Node.js when , It will be executed in you I/O The callback assigned to the operation is reserved during the operation , This allows you to continue with other events . Once all the necessary data has been collected , The callback will be called .
Web The server sends all requests to the event loop , The event loop registers an operation in the thread pool and assigns a callback to it .
Once the request is processed , The callback will be called . Other activities with high requirements , For example, search database , Callbacks can be used to perform .
V8 Role in optimizing code
V8 It consists of two types of compilers :
- Complete compiler
- Crankshaft compiler
The entire compiler is known for its speed and ability to generate general-purpose code . It will also JavaScript Abstract syntax tree of function (AST) Convert to general native Code . At this level , It performs only one optimization : inline caching .
V8 Start the parser thread at code execution time and compile the functions to determine which functions are hot , Which are not . Besides , It also captures type feedback , This helps V8 The record is passing through its data type .
Identifying “ heat ” Function and collect some types of feedback ,V8 Try optimizing the compiler ( Sometimes called “Crankshaft” compiler ) Perform enhanced AST.Crankshaft The compiler is slow , But it will try to generate efficient code . It consists of two compilers :hydrogen and lithium.
Hydrogen The compiler works from AST File creation control flow diagram (CFG). The resulting graph shows a static single allocation (SSA) chart .
With the help of SSA A simple structure of forms and high-level intermediate representations , The compiler can perform many optimizations , For example, method inlining 、 Constant folding, etc .
Lithium The main goal of the compiler is to optimize the HIR Convert to LIR.LIR Very similar to machine code , But it has nothing to do with the platform .
after , old 、 The non optimized code will be replaced by the optimized code . This helps to execute code faster .
5 The best Node.js Use cases
Besides speed ,Node.js It also contains many functions that developers can use in a range of applications . Let's take a look at some classic Node.js Use cases .
Real time applications
When asked “Node.js What is the best use case for ?” Real time applications are a good answer . This is a Node.js A good application . Let's see why Node.js So popular in real-time applications :
- Node.js Support for reusing and sharing for repository code Node.js package .
- It leads to fast data synchronization between the server and the client .
- It can easily handle multiple client requests .
All in all , If real-time data processing and scalability are two important considerations for your project , that Node.js Is the best technology .
More Than This , It should be used in real-time application development Node.js There are many reasons . therefore , If someone wants to know why you chose Node.js To develop real-time applications , That's why .
Event based servers
Multiple real-time users must be handled in real-time applications . This is it. Node.js Where it comes in handy , Because it can handle the response of event driven server , So as to realize non blocking function .
Data synchronization
Node.js Developers use non blocking I/O function . It also allows fast data transfer between clients and servers .
proxy server
Whenever an intermediate administrator is needed ,Node.js It's the ideal choice . To put Node.js As a proxy server , Developers must write 20 Line code . after , Your application will be ideal for streaming data from many sources .
Highly scalable applications
Node.js Has become the preferred platform for all applications and platforms with a steadily growing user base . Wal-Mart 、 PayPal 、 Uber 、Netflix、LinkedIn Companies are using Node.js, Because it allows seamless expansion .
Node.js Its cutting-edge technology includes cluster module and other functions . It provides services across multiple CPU Load balancing of the kernel , It is easier to provide the desired results through smaller modules , Without exhausting RAM.
More Than This ,Node.js There is also a non blocking event loop system , Allow the server to process requests in real time .
In terms of development ,Node.js Allow you to use microservices , This further allows you to break down the application into smaller components .
By effectively defining and assigning tasks between different teams , You can accelerate the development of each component of the application 、 Deployment and maintenance .
With Node.js, Modern applications can be expanded and reduced as needed . It can also help enterprises achieve higher performance with fewer resources .
Server side agents
In online applications , Third party agents can cause serious damage . Many common third-party servers , for example Nginx and HAProxy, Sometimes it is difficult to handle simultaneous requests .
Node.js Can be a savior , Because it can be easily used as a server-side proxy , Because it can handle a large number of simultaneous connections , While keeping non blocking .
BBC News yes NodeJS An example of an application .BBC News websites interact with many third-party servers , And gather information from a variety of sources .Node.js Assist agents with various services with different response times .
If the agent infrastructure is not up to standard , Or you need to develop a solution locally , You can use Node.js.
It means that you can Node.js Development servers are used for assets and stubs API Query to create client applications . In production , image HAProxy and nginx Such a dedicated proxy server can handle such transactions .
Conclusion
With its lightning fast execution speed ,Node.js It has proved to be the Savior of many developers and enterprises . Many big companies take advantage of Node.js This characteristic of . In this article, we explain in great detail what makes Node.js So fast and unique .
边栏推荐
- 为什么 Nodejs 这么快?
- Data driven decision making: Decision intelligence and design thinking
- JS picture switching case
- 时间溯源的系统设计思路
- Restful design method
- Introduction and download tutorial of administrative division vector data
- 云服务器类型怎么选,要考虑什么?
- Vite+web3:报错出现ReferenceError: process is not defined
- How to use Fisher's least significant difference (LSD) in R
- variable
猜你喜欢

How MySQL works - Chapter 14

Get max value of a bit column - get max value of a bit column

Huitongda officially landed at the Hong Kong Stock Exchange: the gross profit margin continued to decline, and the book value of several shareholders still suffered losses

ASP. Net hosting uploading file message 500 error in IIS

This is not safe
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now

Leetcode weekly buckle 281

电源噪声分析

The verifiable certificate of geoscience remote sensing industry

Introduction and download of nine npp\gpp datasets
随机推荐
How to create a linear model prediction interval in R and visualize it
【Leetcode】旋转系列(数组、矩阵、链表、函数、字符串)
subject may not be empty [subject-empty]
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
Value passing and reference passing of value types and reference types in CSharp
Knowledge points in T-SQL
程序员如何做自媒体?
BOM(Browser Object Model)
Crmeb multi merchant PC packaging tutorial
电源噪声分析
Introduction to alos satellite
R language Quantitative Ecology redundancy analysis RDA analysis plant diversity species data visualization
Sentry series satellite introduction and download tutorial
Leetcode weekly buckle 281
JS picture display and hiding cases
建立自己的网站(8)
JS deep understanding of functions
three.js创建的基础框架
Microservice system design -- interface document management design
论文解读(SR-GNN)《Shift-Robust GNNs: Overcoming the Limitations of Localized Graph Training Data》