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:09 3175browse

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 usesNodejs, I have been learningNode.jsrecently. As I gradually deepened my understanding, I found that there is a reason whyNode.jscan become more and more popular. Makes sense. Some people may say,Javahas always been the leader as a back-end language, so why should we learnNode.js?Node.jsWhat exactly is it? Is it a new language or a new framework, a new tool or just a simpleJavaScriptfile?

Related recommendations: "nodejs Tutorial"

Runtime Environment
We all knowJavaHave a runtime environment calledJREto enable java programs to run smoothly.JREThere is a virtual machine calledJVM.JVMhas 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 ofJavaandNodeLooking at the ecological community and support libraries behind it, however, the traditional system withJavaas the core is naturally not as good as new forces likeNode. In short,Javais mature and huge,Nodeis fast and active.

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

But inSpring, 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.Springhas 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 isspringexecuted? The framework needs to parse method names, guess the programmer's intent, build something like an abstract syntax tree, generateSQL, and so on.

How much additional cost are these things? Therefore, usingJavato cover up complexity will not simplify it, it will only make the system more complex.JavaStrict type checking allowsJavato help you avoid many types ofbug, 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.jsis just the opposite. Threads lead to more complex systems. SoNode.jsadopts a lightweight, single-threaded system and usesjs'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 usingNode.js, but asynchronous callbacks naturally present an urgent problem that needs to be solved:Callback Trap.

InNode.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 usePromiseto simplify asynchronous operations, but it seems that we have simplified the problem, but in fact the code level is more complicated.Promiseuses a lot of boilerplate code. Concealing the programmer's true intentions.

LaterNode.jssupportsES5andES6, and the callback function can be rewritten using theasync/awaitfunction. 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 theasyncfunction, the code becomes more beautiful. A simple solution that turns the shortcomings ofNode.jsinto advantages. ButJavaScriptis 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 inNode.jsIn order to better debugBUG,Nodesupports 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,Mavenis completely different from the package management system ofNode.js.

npmis the package management tool officially provided byNode.js. It has become the standard release platform forNode.jspackages. Use ForNode.jspackage publishing, propagation and dependency control.npmProvides 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

npmcode base is not only used byNode.jsbut also by front-end engineers. All front-endJavaScriptlibraries exist in the form ofnpmpackages. Many front-end tools such asWebpackare written inNode.js.

Performance

JavaUseHotSpotThis 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. ThereforeHotSpotperformance is relatively faster.

NodeThe bottom layer is implemented using thecandv8engines, the event-driven mechanism ofNode.js, This means that in the face of large-scalehttprequests,Node.jsis accomplished by event-driven, so there is no need to worry about the performance part, and it is excellent. And, thanks to improvements in theV8engine, every release ofNode.jswill bring huge performance improvements.

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

  • Nodeis not suitableCPUIntensive applications, because ifCPUintensive applications have long-term operations, it is not as good as a large loop, which will cause theCPUtime slice to not be released, making subsequentIOAll operations are suspended.

  • And

    Nodeonly supports single-coreCPUand cannot fully utilizeCPUresources.

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

    Nodeuses 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