Node.js and PHP are two of the most popular server-side platforms for developing website content and functionality.
While each of these platforms has its devotees and detractors, as well as advantages and disadvantages, one of the areas in which they compete most heavily is performance.
Today, I’ll walk you through a detailed comparison of Node.js vs PHP performance, as well as some other features to consider.
What is Node.js?
What is PHP?
PHP, which stands for ‘PHP: Hypertext Processor’, is a scripting language in and of itself. The PHP code can be embedded in HTML or processed by interpreters that come standard with the Apache and Nginx web server engines. Because of this, and its long 25-year history, PHP is used on more than 80% of web servers.
Node.js vs PHP: How Do They Compare on Performance?
Node.js and PHP have fundamental differences in how they operate. That means there should be a difference in how they perform for loading website content. I'll walk you through some basic performance differences and highlight three benchmark speed tests to compare the two platforms.
When it comes to compiling the code, Node.js has a slight advantage over PHP. That’s because PHP operates an interpreted compilation strategy, which means that it relies on a separate interpreter to compile and run a block of code. On most web servers, PHP is operated and parsed using a Zend runtime environment.
That said, the HHVM virtual machine can dramatically increase PHP’s compilation speed by providing a runtime environment specifically for PHP. Like Node.js, HHVM operates using Just-in-Time compilation rather than the slower, interpreted compilation normally used to run PHP code.
PHP, on the other hand, uses a blocking process that requires multiple CPU threads when multiple client requests are made to the server. As a result, when many simultaneous requests are made to the server, the entire server can become CPU-limited in dealing with the interpretation of the PHP code. When this happens, the time to complete all of the requests can skyrocket.
There are some redeeming qualities to PHP’s multitasking capabilities when running Apache or Nginx web servers with performance-enhancing PHP extensions. However, these are limited to speeding things up and rerouting tasks to better distribute the available computing power. Ultimately, Apache and Nginx still don’t fundamentally improve the ability of PHP to deal with simultaneous tasks.
Benchmark 1: Basic HTTP Request
When making a simple ‘Hello World’ HTTP request using either Node.js or PHP, the speed advantages of Node.js are clear. When PHP is run inside an HHVM virtual environment, it speeds up PHP rendering by about 74% compared to a standard Zend runtime environment. However, even with this speed increase, PHP execution is still approximately 17% slower than Node.js.
Benchmark 2: Running a Bubble Sort
This is especially noticeable as the number of threads required for the bubble sort begins to exceed the number of threads available on the webserver.
In this case, PHP and Node.js perform similarly until the webserver is tasked with 1,000 processes. By 10,000 simultaneous processes, though, Node.js performs roughly twice as fast as PHP and HHVM.
Benchmark 3: CombSort Sorting
CombSort sorting is a numerical sorting test similar to bubble sorting, but it more directly measures the efficiency of RAM utilization. In this benchmark test, PHP running in a standard Zend runtime environment was 35 times slower than Node.js. Even when PHP was run inside an HHVM runtime environment, Node.js remained five times faster at the sorting task.
Node.js Vastly Outperforms PHP
When it comes to Node.js vs PHP performance, there’s little question that Node.js is superior. Node.js outperformed PHP in all three of my benchmark tests, even when PHP was tested inside the high-performance HHVM runtime environment.
That means that when it comes to executing code on your web server and delivering content to visitors, Node.js is going to be faster almost every time.
More importantly, the difference between Node.js vs PHP was particularly noticeable when multiple simultaneous requests were made to the web server. For shared hosting servers or dedicated servers that see a high volume of web traffic, that difference in multitasking performance can be the difference between delivering content efficiently and a crashed server.
That said, there are considerations other than performance to take into account when choosing between Node.js and PHP. PHP is still much more widely used and has a much more established community of developers, so it’s not likely to disappear anytime soon. That said, Node.js is likely to become more important as more developers shift to take advantage of its high performance.
Did this guide help you understand the difference between Node.js vs PHP performance? Let me know in the comments below!