Press "Enter" to skip to content

Node.js vs PHP

Let’s distinguish between PHP and Node.js. PHP is a programming language that powers most of the websites on the world wide web. That’s 244 million websites [3] or 81.7% of websites that use server-side scripting [4]! Note that PHP is only one of the components of the AMP stack (Apache-MySQL-PHP), a suite of software that collectively provide an environment for web services. Node.js, on the other hand, is a run-time environment. The table below (Figure 1) somewhat illustrates the ‘layers’ of a web application based in either PHP or Node.js.

Webpage (HTML/CSS/Javascript)
PHP libraries Node.js NPM libraries
PHP Javascript
Apache / Nginx / Lighttpd Google V8 Engine
Operating system on host server (Linux, Windows, etc.)

Figure 1.

 

PHP – The All-Time Favorite?

PHP is built directly into the web server [8]. In only a handful of lines of code, you read data, process data, and display the results as part of a dynamically generated web page. PHP is the most popular and widely used platform for a number of reasons; you could write a simple procedural program in PHP with a few lines of code (no strong type checking), or build an enterprise-level application with all the properties of a C-style object-oriented language. In other words, it is very flexible, scalable, and the entry barrier is very low. It is also cheaper to produce web applications in PHP. There are plenty of hosting solutions, content management systems, and open source frameworks & libraries available for applications written in PHP.

Rasmus Lerdorf, the creator of PHP, started work on the language because he wanted to spend less time writing CGI programs for web services [5, 6]. He used to write his CGI programs in C and Perl, and these programs also contained HTML code, and writing all this code made the work cumbersome. Apparently, this was the structure of an early version of PHP (Figure 2):

PHP (binary executables) C program for HTML templating
C library for CGI
NCSA HTTPd web server

Figure 2

Because PHP is built on C and PERL, it can leverage the powerful capabilities it inherits. However, this is also why the language is so syntactically inconsistent. This language inconsistency has drawn a lot of criticism, and it’s an issue Lerdorf himself is aware of. He does, however, make the fair argument that PHP maintains great documentation [5].

 

Slide2

Figure 3.

 

Node.js – The Dark Horse

The motivation for Node.js, as described by its creator Ryan Dahl, was to make web servers faster, less resource intensive, and provide improved file IO capabilities [2]. In fact, it was the Flickr file uploader and its RESTful progress bar that inspired Dahl to create Node.js [2]. Dahl believed that because some web servers, the likes of which Ruby and PHP run on, were inefficient because they used separate threads for every task, and requests were blocked until the server responded (Figure 3). Node.js is different than PHP in that it uses only a single thread to process all of the server requests (Figure 4) and is non-IO blocking – and this is the fundamental reason why Node.js performs better than PHP virtually in all server-side operations [9].

Node.js is so called because a lot of its codebase is written in Javascript. Because Javascript is primarily associated with the DOM in web browsers, it may surprise some when they learn that Node.js modules for web services are written entirely in Javascript. It important to note here that Javascript can be used for applications outside of the web browser – Node.js is only one such example. The DOM API is not inherent to Javascript. However, Javascript is the de facto language for implementing the DOM API.

Node.js runs on the Google V8 Javascript engine. In Google V8, Javascript is compiled directly to machine code instead of being interpreted in a webpage. This is an important reason why the Chrome browser, which is powered by the Google Javascript V8 engine, is such fast web browser.

Although small in size, Node.js is modular and can include components that allow it to perform tasks much like PHP; listening to HTTP and TCP ports, making server requests, and accessing file systems [1]. The Node Package Manager (NPM) includes modules for database connections and operations (Express.js, for example).

Another advantage with Node.js as the platform for the application is that one language (Javascript) is primarily used both on the frontend and the backend. A one language ecosystem could potentially eliminate the backend-frontend divide and all the problems that come it.

Figure 4 [1]

 

Footnotes:

  1. Node.js is compiled with Just-in-time approach. In Just-in-time compilation, there is a slight delay in the initial execution of a program.

References:

  1. B. Terkaly, & R. Villalobos, 2013, Azure Insider – Real-World Scenarios for Node.js in Microsoft Azure, Retrieved from
    https://msdn.microsoft.com/en-us/magazine/jj991974.aspx?f=255&MSPPError=-2147217396
  2. phxtagsoup’s channel, 2011, History of Node.js, Retrieved from https://www.youtube.com/watch?v=SAc0vQCC6UQ
  3. PHP usage stats, Retrieved from
    http://php.net/usage.php
  4. Usage of server-side programming languages for websites, Retrieved from http://w3techs.com/technologies/overview/programming_language/all
  5. fwdays, PHP Frameworks Day 2013, Retrieved from https://www.youtube.com/watch?v=anr7DQnMMs0
  6. ieeeComputerSociety, Computing Conversations: Rasmus Lerdorf Interview on Creating PHP, 2012, Retrieved from https://www.youtube.com/watch?v=YIGRXEzjE6c
  7. PHPNW, PHPNW14: Davey Shafik – PHP: Under the Hood, Retrieved from https://www.youtube.com/watch?v=CYfpfAt0pU8
  8. P. Simons, & R. Babel, 2002, FastCGI — The Forgotten Treasure, Retrieved from http://www.nongnu.org/fastcgi/
  9. I. K. Chaniotis, K. D. Kyriakou, & N. D. Tselikas, 2014, Is Node.js a viable option for building modern web applications? A performance evaluation study

Be First to Comment

Leave a Reply