In the online development world, Node.js and PHP are some of the most popular platforms used. Everybody wants to know about Node.js vs PHP Performance. We are trying our best to give you all possible information as per our expert’s knowledge.
PHP was first released in 1995, followed later by Node.js in 2009, and they are both mainly used as a server-side scripting language for developing web-based applications. However, Node.js is also starting to be used as a desktop application development platform. If you want to Hire PHP Developer, you can get in touch with us.
NODE.JS VS PHP PERFORMANCE
In this article, we will take a look at the performance differences between these two platforms, some popular PHP extensions to improve performance, such as the Zend JIT runtime and HHVM PHP runtime project, and some third-party benchmarks of these extensions.
THE BASICS OF NODE.JS VS PHP PERFORMANCE
One area where there has been a healthy competition of sorts between PHP and Node.js is in the area of performance.
PHP – INTERPRETED COMPILATION
The standard PHP Zend runtime uses an interpreted compilation strategy, leading to less optimized code execution than a good JIT-(Just-in-Time)-based runtime.
NODE.JS – JUST-IN-TIME COMPILATION USING V8
INCREASE PHP PERFORMANCE: OPCACHING, JIT COMPILATION, AND HHVM
Several popular extensions have been developed — designed to improve PHP performance.
OPCACHING IN PHP
Zend PHP 5.5+ also has a caching engine called OPcache, which stores precompiled script byte code in shared memory, thereby removing the need for PHP to load and parse scripts on each request. This is not as performant as JIT compilation but is a stable feature that is working now. You can enable PHP 5.5 OPcache if for some reason it’s not enabled by default (as it should be with PHP 5.5 or higher).
PHP RUNTIME ALTERNATIVES: HHVM & HACK
HHVM is an open-source virtual machine designed for running applications written in PHP and/or the Hack language.
To make the most of the HHVM JIT compilation, it is important to put your code into functions, especially for the more time-consuming code parts.
The Hack language allows for performance improvements mainly due to these language features:
JIT COMPILATION IN FUTURE PHP RELEASES
There has been discussion and development to use JIT compliation in a future release of the PHP Zend runtime. JIT for PHP provides more than five times speedup on bench.php and as high as a 30 times improvement on some other specific benchmarks like Mandelbrot.
MULTI-TASKING IN NODE.JS AND PHP
Node.js and PHP differ in their approach to concurrency, with Node.js using a non-blocking event loop (running in a single process) and the standard PHP Zend runtime using a blocking process.
For this reason, multiple PHP processes are often launched from the web server to be able to keep serving web requests. In Apache, you can set the maximum number of PHP processes allowed to run, and this works fine for many types of web applications.
Now, when the work being done in these processes tends to be long (er)-running, the server can quickly run out of available PHP processes. This will cause the HTTP request response times to increase, perhaps even to the point of requests timing out.
Web servers like Apache and NGINX have ways to improve the PHP multi-tasking performance to some extent, but these are more limited than true language support for multi-tasking.
There are several projects being worked on to make PHP asynchronous processing (multi-tasking) works better. The most popular are the HHVM project released in 2014 by Facebook; another project tackling PHP concurrency is threaded.
NODE.JS VS PHP PERFORMANCE BENCHMARKS
So what might these PHP extensions bring us in terms of performance compared to standard PHP or Node.js?
Let’s have a look at a few different benchmarks people have done.
BENCHMARK 1: SIMPLE HTTP REQUESTS
Click on the image to go to the benchmark website and see the full setup details.
BENCHMARK 2: HTTP + CPU TASKS
BENCHMARK 3: COMBSORT STRICT CPU TEST
The following CombSort sorting benchmark is a strict CPU test.
The benchmark results:
HHVM is seven times faster than plain PHP (by system time). However, Node.js is more than five times faster than HHVM in this number-crunching test.
In terms of RAM usage, HHVM is much more efficient than PHP, but Node.js is even better.
FINAL THOUGHTS ON NODE.JS VS PHP PERFORMANCE
For most PHP applications, HHVM clearly provides a significant performance boost thanks to the JIT compilation. The same will likely be true when the Zend runtime JIT feature is stable and released. How much the PHP JIT compilation improves upon the non-JIT PHP is also a matter of code organization (especially the time-consuming code parts).
At best, HHVM is on par with Node.js, but in some cases, Node.js will clearly perform much better (e.g., for CPU-bound tasks). There are also more async code modules available for Node.js.
There is an ongoing discussion about the future of the Zend and HHVM PHP runtime. HHVM also looks to be getting even faster by using LLVM later this year. That’s all about Node.js vs PHP Performance.