At Krizalys, we are huge fans of Node.js. We believe it introduced many new concepts that turned web development into a more efficient process and a strong potential to become the leader on the market.
Among other things, Node.js provides an incredible performance, either in rendering pages or actually developing them. This article outlines key performance aspects of Node.js, and in most cases, compare them to a well-known competitor: PHP.
Pages render fast because of...
A monolithic web stack
For modularity reasons, most web frameworks use different software to handle different tasks. For example, a LAMP stack provides, among other things, the Apache web server to handle HTTP traffic and the PHP engine to run web applications.
On the other hand, a Node.js web application typically bundles both the web server and the web application itself. By reducing the overhead of having two software handling different tasks and communicating to each other, this translates to shorter response times.
A "run once" approach
Because a typical Node.js web application bundles a server web, it is designed to never exit. Instead, at the core of the application, you write a callback function to handle incoming HTTP requests, and this function is called as many times as there are requests.
Compare this to PHP applications, where the engine is often running as a CGI for security reasons, and will need to initialize itself, run the code, and deinitialize itself for each request.
A "require once" approach
In contrast, in PHP, each request will run the same code to include the same PHP libraries over and over again, and the overall performance of the application is impacted because of the higher I/O operations.
A "connect database and prepare once" approach
Second, for Node.js applications using SQL databases, developers have the ability to connect database servers and prepare SQL queries once, and execute prepared queries multiple times to satisfy multiple the requests. Connecting to database servers only once and running only prepared queries decreases the overhead implied by the database server.
In some SAPIs of PHP, it is not possible to open real persistent connections, and when it is, the database driver has to support it. Furthermore, while it is possible to prepare SQL queries in PHP, if the query is prepared for each request , there are two round-trips to the database server, and unless the query is run multiple times for the same request, there is no real benefit in doing so. Indeed, doing this is even slower than not preparing the query at all.
A true compilation model
In PHP and many other scripting languages, the execution process is split in two: compiling to an intermediary bytecode first, and interpret this bytecode by a virtual machine then. While this offers a better portability, the time needed to compile to bytecode is approximately similar to compile to machine code, but in the first case, there is a need to go into a second step of running additional software to actually interpret the bytecode. This is less efficient.
A shorter parsing time
Development is faster because...
You write code in one language, but run it everywhere
According to the amount of code that may actually be shared between both the server-side and the client-side, it is theoretically possible to divide by up to two the time needed to write an entire web application.
When using any other programming language on the server-side, you expose yourself to the risk of needing to write the same logic in two different languages. Additionally, this makes bugs harder to fix if they have been duplicated on both sides.
You learn one language, but are productive everywhere
Another advantage of using Node.js is that developers need to learn only one programming language before becoming proficient on both the server-side and the client-side. If learning or mastering technologies involved is considered part of the development process, this effectively contributes in decreasing the overall development time.