Why JavaScript

The founders saw two key points in a Web server, event-driven and non-blocking I/O.

Why Node

The original founders wanted it to be called Web.js, or a Web server, but the project grew beyond his original idea of a web server to become a basic framework for building web applications.

What Node brings to JavaScript

V8 has given Chrome a strong heart and has become a major weight in the JavaScript language called Node.

The following figure shows the differences between Chrome and Node:In browsers, besides v8 engine, there is also a WebKit layout engine. HTML5 allows browsers to open up a lot of functions to front-end HTML and JavaScript, but HTML5 unification is relatively slow. JavaScript as a Turing-complete language, Limited by the support provided by the browser middle tier. Node’s structure is very similar to Chrome’s, except that UI-related technologies like HTML, WebKit, and graphics are not supported.Both are event-driven, one-step architectures where browsers serve interface interactions and Node serves I/O through event drivers.Node allows JavaScript to access local files at will, build websocket servers, connect to databases, and use multiple processes. JavaScript is no longer limited to browsers and CSS stylesheets and DOM trees. Node doesn’t handle the UI, but runs with the same mechanics and principles as the browser. Breaking the fact that JavaScript can only run in a browser reduces the cost of context exchange required for back-end transformations.

The characteristics of the Node

Asynchronous I/O

The front-end is commonly used to request data through AJAX, there is a time interval between sending and receiving data, and the subsequent code is executed immediately after receiving data, but the time of receiving is uncertain, which is a kind of performance that only pays attention to the result, not the process. As shown in the figure below.Asynchronous I/O is also common in Node, using reading a file as an example:

const fs = require('fs'); fs.readFile('.. /Desktop/IMG_3648.jpeg', (err, file) => { console.log(file); Console. log(' file read finished '); }); Console. log(' Initiate file reading process! ');Copy the code

The example “process of initiating file reading” in the code is executed before “file reading completion”. When it is completed, based on verifying the complete read content, the execution process is shown as follows:In Node, parallel I/O operations occur naturally at the language level, without waiting for previous I/O calls to complete between calls, and the slowest reading of two files depends on the time it takes to read the slowest file. For synchronous I/O, it is a synthesis of two files.

Events and callback functions

The event programming method has the advantages of being lightweight, loosely coupled and focusing only on physical points. However, in the scenario of multiple asynchronous tasks, events are independent from each other and how to cooperate is a problem. Functions, as first-class citizens of JavaScript, pass functions as objects to methods to call as arguments. The callback function is the best way to accept the data returned by an asynchronous call.

Single thread

Node maintains the single-threaded nature of JavaScript. The biggest benefit of single-threaded JavaScript is that it doesn’t have to worry about state synchronization everywhere, there are no deadlocks, and there are no performance costs associated with thread context exchange. Disadvantages of single threading:

  • Unable to utilize multi-core CPUS
  • An error can cause an entire application to quit, and the robustness of the application is tested.
  • A large number of calculations occupy the CPU, causing asynchronous I/O calls to fail

To address these issues, Google developed Gears, the HTML5 standard added the Web Workers standard, and Google dropped Gears support for Web workers. Web Workers can create threads to perform computations and solve the problem of large JavaScript computations blocking UI rendering. Node takes the same approach as Web Workers to solve the problem of large computations in a single thread: child_process.

cross-platform

The original Node was only available on Linux, but support for Windows has since been added.

Node Application Scenarios

I/O intensive

The I/O intensive advantage lies in Node’s ability to take advantage of time loops rather than starting each thread to service each request, which consumes very little resources, so Node is network oriented and good at parallel I/O.

Support for CPU intensive services to some extent

Support for CPU-intensive services on Node is not terrible, but it does present some challenges: As a single-threaded language, JavaScript will occupy a long time for CPU intensive services to compute, resulting in CPU time slices cannot be released and subsequent I/O cannot be initiated. However, the large computing tasks can be properly adjusted and decomposed into multiple small tasks, so that the operations can be released in time without blocking the initiation of I/O calls. In this way, you can enjoy the benefits of asynchronous I/O while making full use of the CPU.

For pure CPU services, you can use the CPU in either of the following ways

  • Node makes efficient use of the CPU by writing C/C++ extensions.
  • If a single-threaded Node is not enough, even with C/C++ extensions, you can use Forbidden City to separate I/O from computation by using a portion of Node processes as resident server processes and then passing messages between processes.

Live in peace with legacy systems

Node can take advantage of parallelism by using an old interface as a data source, regardless of the language behind the implementation.

Distributed application

The process of Node using I/O efficiently is also the process of using the database efficiently. For Node, using the database is just a normal I/O. For the database is a complex calculation, but also fully squeeze the process of hardware resources.

The user

  • Unified front-end and back-end programming language environment, such as yahoo
  • Node provides high-performance I/O for real-time applications, such as Voxer real-time voice
  • Parallel I/O enables users to utilize distributed environments more efficiently.
  • Parallel I/O leverages stable interfaces to improve Web rendering capabilities.
  • Cloud computing platforms provide Node support.
  • Game Development
  • Tool Applications