Comparing Node Js Vs Php Performance

Comparing Node.js vs PHP Performance

Written by: Roberto Sanchez

Roberto Sanchez

Roberto has been a professional full-stack developer for more than 10 years. Triaging and resolving the most mysterious bugs found in code is one of his many technical specialties. He loves experimenting with emerging programming languages and tools — and sharing his experience with the HostingAdvice readership.

See full bio »

Edited by: Lillian Castro

Lillian Castro

Lillian brings more than 30 years of editing and journalism experience to our team. She has written and edited for major news organizations, including The Atlanta Journal-Constitution and the New York Times, and she previously served as an adjunct instructor at the University of Florida. Today, she edits HostingAdvice content for clarity, accuracy, and reader engagement.

See full bio »

In the online development world, Node.js and PHP are some of the most popular platforms used.

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.

PHP began as a dynamic language to update HTML pages with sytax similar to Perl, and started off as a rather basic programming language. Node.js used the already well-established JavaScript language and created a new development platform on top of the V8 JavaScript engine. Over time, PHP got more and more advanced programming features, such as an object-oriented programming model, namespaces, package management, and exceptions.

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 PHP and Node.js 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

Node.js uses Just-in-Time compilation with the V8 JavaScript Engine. This strategy for dynamically-typed languages has proven itself, but does often increase startup time a little.

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 bytecode 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.

HHVM uses a Just-in-Time (JIT) compilation approach to achieve better performance. Node.js also does JIT compilation using the V8 JavaScript runtime.

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 processess 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 processess allowed to run, and this works fine for many types of web applications.

Now, when the work being done in these processess tends to be long(er)-running, the server can quickly run out of available PHP processess. 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) work better. The most popular is the HHVM project released in 2014 by Facebook; another project tackling PHP concurrency is pthreads.

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.

Node.js vs. PHP performance and requests handled per second

Notes:

Benchmark 2: HTTP + CPU tasks

Click on the image to go to the benchmark website and see the full setup details.

Node.js vs. PHP Performance with CPU heavy task of running a bubble sort

Notes:

Benchmark 3: CombSort Strict CPU Test

The following CombSort sorting benchmark is a strict CPU test.

The benchmark results:

CPU time System time RAM
PHP 5.6.4 102.69s 104.20s 2497508 KB
HHVM 3.5.0 12.56s 14.83s 362488 KB
Node.js v0.10.35 2.64s 2.64s 92240 KB

HHVM is seven times faster than plain PHP (by system time), but 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 and 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 organzation (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 ongoing discussion about the future of the Zend and HHVM PHP runtimes. HHVM also looks to be getting even faster by using LLVM later this year.

If you are interested in monitoring your PHP application performance better, perhaps have a look at these performance dashboard solution providers:

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.

ABOUT THE AUTHOR

Roberto Sanchez

Roberto Sanchez got his start with web hosting when he was still in high school, with a Perl script to conjugate verbs on a shared host. Roberto has been involved with web development ever since, experimenting with many web languages and hosting types. Roberto is on the web development team at Digital Brands Inc., where he applies his knowledge of PHP and WordPress to improve performance for the firm's various web properties. Roberto also leverages NodeJS to develop exciting products at Digital Brands Inc. and is eager to share his experience with HostingAdvice.com users.

« BACK TO: BLOG
Follow the Experts
We Know Hosting

$

4

8

,

2

8

3

spent annually on web hosting!