Home  >  Article  >  Web Front-end  >  A simple comparison between Node.js and java backend servers

A simple comparison between Node.js and java backend servers

青灯夜游
青灯夜游forward
2020-12-21 18:00:093207browse

A simple comparison between Node.js and java backend servers

Recently I went to a new company and picked up the backend that I had left behind for a long time. However, due to the needs of the company, the backend uses Nodejs, I have been learning Node.js recently. As I gradually deepened my understanding, I found that there is a reason why Node.js can become more and more popular. Makes sense. Some people may say, Java has always been the leader as a back-end language, so why should we learn Node.js? Node.jsWhat exactly is it? Is it a new language or a new framework, a new tool or just a simple JavaScript file?

Related recommendations: "nodejs Tutorial"

Runtime Environment
We all know JavaHave a runtime environment called JRE to enable java programs to run smoothly. JREThere is a virtual machine called JVM. JVM has many components such as garbage collector (GC), just-in-time (JIT) compiler, interpreter, class loader, thread manager, exceptions Processor, used to perform different tasks at different times. JREThere is also a series of libraries to help run-time Java programs.

Why do we suddenly involve the JRE runtime environment? In fact, it is to compare with Node. Node is not a language, a framework, and a tool. It is a way to run JavaScript applications. runtime environment. Node.js has a virtual machine called JavaScript Virtual Machine. It generates machine code for JavaScript-based applications to enable it on different platforms. This virtual machine is Google's V8 engine, and it also has major components, such as JIT and GC, which are used for task execution, runtime compilation, and memory management respectively.

Development potential

It may be necessary to judge the development potential of Java and Node Looking at the ecological community and support libraries behind it, however, the traditional system with Java as the core is naturally not as good as new forces like Node. In short, Java is mature and huge, Node is fast and active.

Java Needless to say about its functionality and practicality, but Java contains a large amount of sample code, which disrupts the intention of the programmer. It is not as good as spring, one of the three major frameworks of Java. When programmers use spring, servlet, data persistence, and the components that make up the system The underlying things, the spring framework has been encapsulated and will help you handle it all. We only need to focus on writing business layer code.

But in Spring, subsystems follow one another, and even if you make the tiniest mistake, it will punish you with exceptions that crash you. You may see a huge exception message immediately afterwards. It contains encapsulated methods that you don't even know about. Spring has done a lot of work to realize the functions of the code.

This level of abstraction obviously requires a lot of logic, and long exception messages are not necessarily a bad thing. It points out a symptom: how much additional overhead in memory and performance does this require? How is spring executed? The framework needs to parse method names, guess the programmer's intent, build something like an abstract syntax tree, generate SQL, and so on.

How much additional cost are these things? Therefore, using Java to cover up complexity will not simplify it, it will only make the system more complex. Java Strict type checking allows Java to help you avoid many types of bug, because bad code cannot be compiled.

The disadvantage of Java's strong typing is that there is too much boilerplate code. Programmers have to constantly perform type conversions. Programmers have to spend more time writing precise code and use more boilerplate code in order to find and correct errors early.

And Node.js is just the opposite. Threads lead to more complex systems. So Node.js adopts a lightweight, single-threaded system and uses js's anonymous function for asynchronous callback. You only need to simply use the anonymous function, which is a closure. There is no need to search for the correct abstract interface, just write down the business code without any redundancy. This is the biggest benefit of using Node.js, but asynchronous callbacks naturally present an urgent problem that needs to be solved: Callback Trap.

In Node.js, when we continue to nest callback functions, it is easy to fall into the trap of callback functions. Each layer of nesting will make the code more complex, making error handling and result processing More difficult. A related problem is that the JavaScript language does not help programmers express asynchronous execution appropriately. In fact, some libraries use Promise to simplify asynchronous operations, but it seems that we have simplified the problem, but in fact the code level is more complicated. Promise uses a lot of boilerplate code. Concealing the programmer's true intentions.

LaterNode.js supports ES5 and ES6, and the callback function can be rewritten using the async/await function. It's still the same asynchronous structure, but written using a normal loop structure. Error and result handling are also placed in a natural place, the code is easier to understand, easier to write, and the programmer's intent can be easily understood. Callback traps are not solved by masking complexity.

Instead, language and paradigm changes solve the problem of callback traps while also solving the problem of too much boilerplate code. With the async function, the code becomes more beautiful. A simple solution that turns the shortcomings of Node.js into advantages. But JavaScript is very loosely typed. And no errors will be reported when you write code, many types do not need to be defined, and usually there is no need to use type conversion.

Therefore, the code is clearer and easier to read, but there is a risk of missing coding errors. Only during compilation will your grammar and logic be checked for problems, so in Node.js In order to better debug BUG, Node supports dividing the program into different modules. Because of the existence of modules, the scope of error occurrence is reduced to a certain range, makingNode.jsModules are easier to test.

Package Management

One of the most important problems is that there is no unified package management system. Some people may Talk to me about Maven. But in terms of purpose, ease of use, and functionality, Maven is completely different from the package management system of Node.js.

npm is the package management tool officially provided by Node.js. It has become the standard release platform for Node.js packages. Use For Node.js package publishing, propagation and dependency control. npm Provides command line tools that allow you to easily download, install, upgrade, and delete packages. It also allows you to publish and maintain packages as a developer.

The best part is that the

npm code base is not only used by Node.js but also by front-end engineers. All front-end JavaScript libraries exist in the form of npm packages. Many front-end tools such as Webpack are written in Node.js.

Performance

JavaUse HotSpotThis super virtual machine, which uses multi-word section compilation strategy. It detects code that is executed frequently, and the more times a piece of code is executed, the more optimizations it will apply. Therefore HotSpot performance is relatively faster.

NodeThe bottom layer is implemented using the c and v8 engines, the event-driven mechanism of Node.js, This means that in the face of large-scale http requests, Node.js is accomplished by event-driven, so there is no need to worry about the performance part, and it is excellent. And, thanks to improvements in the V8 engine, every release of Node.js will bring huge performance improvements.

Although Node has extremely high performance for high-concurrency applications, Node.js also has its own shortcomings:

  • Node is not suitableCPU Intensive applications, because if CPU intensive applications have long-term operations, it is not as good as a large loop, which will cause the CPU time slice to not be released, making subsequent IOAll operations are suspended.

  • And

    Node only supports single-core CPU and cannot fully utilize CPU resources.

  • The reliability is low. Once a certain link of the code crashes, the entire system will crash. The reason is that

    Node uses single entry

Procedure.

  • Node's open source component library has uneven quality, is updated quickly, and is not backward compatible.
For more programming-related knowledge, please visit:

Introduction to Programming! !

The above is the detailed content of A simple comparison between Node.js and java backend servers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete