How efficient NodeJS compared to PHP?

<span style="color: rgb(51, 51, 51);">Many benchmarks have shown that PHP is as fast as Node.JS. In fact many people mistake Node's async and single threaded design is a performance booster, but this notion is misconstrued. What Node really provides is a very practical and optimized solution for web development.&nbsp;</span>

Many benchmarks have shown that PHP is as fast as Node.JS. In fact many people mistake Node's async and single threaded design is a performance booster, but this notion is misconstrued. What Node really provides is a very practical and optimized solution for web development. 

Any Java or PHP framework with a good mutex implementation will beat Node.JS and sooner or later one will catch up(most likely that it would be Java not PHP maybe never PHP).

However even if any of these languages catch up will they really replace Node.JS, I don't think so, because developers who have understood Node.JS for what it is doesn't love it for performance but because of its really efficient program design that is highly functional for web development or server development of any kind.

While some benchmarks show that Node.JS is clearly faster like the one below

Change(b)log: Benchmarking Node.js - basic performance tests against Apache + PHP

Then there are other that show PHP is still quite fast

TechEmpower Web Framework Performance Comparison

And it all depends upon scenario

Where Node would be faster than PHP

1) If requests are I/O bound - Node's async is exactly designed for this purpose

2) If requests are CPU bound but still not very complicated - Node is still quite fast and V8 engine is very optimized to perform simple CPU bound operations

Where PHP would be faster than Node

1) Complex operations - PHP will beat Node squarely because even with V8 Node still have to deal with performance limitations of Javascript

2) handle business logic and render html at the same time - You cannot use Node to vomit HTML pages, that'll defeat the purpose of using Node. You'll definitely use some templating engines, employ separation of concern and build a lot of infra around your business logic to have a good solution in Node.

But a better question to ask before performance is Productivity. C, C++ and GO are very fast that PHP, Java and Node.js but will you use them to build your sites. Python and Ruby are damn slow but a lot of high traffic sites like Twitter, Reddit, Dropbox, Instagram use them.

And this how I'll rank frameworks when it comes to productivity

1) PHP - has awesome developer base and you can literally dish-out any kind of site in not time using Wordpress, Zend, Laravel, Joomla.

2) Ruby On Rails - what Rails lacks in performance it more than makes up for it with a die-hard developer base and precious Gems. And unlike PHP you don't have to deal with different frameworks like Wordpress to be extra productive, you can literally write away anything in no-time using Rails and that is why it has such high adoption, because developers who like to crank code than copy paste love Rails.

3) Python - highly productive language and enough said. People gravitate towards Python for one simple reason PYTHON. Like Ruby, developers who like to crank code loves Python, but unlike Ruby, Python has one more reason why it has such wide spread adoption, it is multi-paradigm and slowly becoming the first language for the next generation of developers. Yes kids these day learn Python before the learn they learn C.

Also many experimental projects are done on Python and when these projects mature and becomes viable enough to be launched as a website, developers stick to the language. That is how many popular sites like Reddit, Quora which employs Analytics and learning algorithmd ended up using Python.

4) Node.JS - technically Node.js is superior for web development than Python. It has an excellent package manager and gives you access to thousands of libraries that have ready made solutions for 90% of requirements of an average website. But Python will beat it squarely when it comes to raw productivity. Also Python has mature testing frameworks than Node, it can be wielded for scripting, integration middlewares, glueing services and comes with an all round package.

5) Java - Java is still very fast and has a very mature ecosystem. And Java developers are as productive as Node developers. However when it comes to productivity Python & Rails(not Ruby) are still superior choices. You cannot compare PHP, it is in completely different league.

Wrong Choices

1) Go - with Google's marketing and the buzz around Internet of Things a lot of people are eyeing on Go. But IMHO it is a wrong choice for web development. Go from a practical perspective would be very useful in very specific cases where performance is mission critical and you need a flexible language to do it. Go supports concurrent, asynchronous/synchronous, single threaded and multi threaded programming models, and its very fast and clean.

But developing a website with Go would be like developing it with an upgraded version of C.

2) ASP - I'm going to get a lot of trolling for mentioning this, but before trolling please try Rails or Python and you'll get the picture. ASP was Microsoft's stupid answer for PHP & JSP and even after all these years PHP will squarely beat it. Neither it has the style and flair of Rails nor it is as productive and flexible as Pythonic frameworks.

3) Scala/Erlang/Clojure/Dart - all these languages have dedicated fan base but when compared with the above choices they are very bad options. For web development, Node provides a more practical concurrent model, Ruby and Javascript provides a more practical functional programming language and Python gives crazy productivity.


Getting developers onboard is a big factor for successful websites and the above languages will fall flat.

Node.js vs PHP: Which is better for Backend Developer?

Node.js vs PHP: Which is better for Backend Developer?

PHP and Node.js are both powerful backends for dynamic websites. In this article, we will be talking about the key features & differences between Node.js and PHP, and also try to identify which technology is suitable for which purpose.

PHP and Node.js are both powerful backends for dynamic websites. They both fall under the same category, yet their features are quite distinct. There’s no doubt — PHP is the most known and commonly used language for server-side scripting. However, Node.js made it possible to use JavaScript programming on the server side when it was introduced in 2009, fueling the rise of sites with entirely JavaScript-powered stacks for front-end as well as back-end. Before Knowing their differences, let’s first see What’s the Node.js & Php.

PHP

PHP: Hypertext Preprocessor is a general-purpose programming language originally designed for web development. It was originally created by Rasmus Lerdorf in 1994, and ever since has been used as the first choice of language preference for content management systems like WordPress, Drupal and Joomla. As of the latest statistics of 2018, more than 80% of websites are built with PHP.

Node.js

Node.js is an open-source, cross-platform, JavaScript runtime environment, built on Chrome’s V8 JavaScript engine, that executes JavaScript code outside of a browser. It was created in 2009 and came up with the main advantage — Node.js allows to perform asynchronous programming. Although the percentage of websites that are built with Node.js is comparatively low (0,4%), it’s fastly becoming popular among developers.

Synchronous code executes line by line and proceeds to execute the next line of code when the current line has been executed.

Asynchronous code executes all the code at the same time.

Node.js vs PHP: Differences

Runtime environments :

While both JavaScript and PHP can be embedded directly into HTML, they both need an interpreter in order to run. PHP has long been readily straightforward to install and use on the server-side, and is powered by the Zend engine. Node.js is a runtime environment for JavaScript on the server side, powered by Google’s V8 JavaScript engine.

Simplicity :

PHP is conceptually much simpler to use than Node.js. When setting up a server, all you need is a “.php” file with some code wrapped between the tags, enter the URL into your browser, and you’re done. Behind the scenes, a web server like MySQL with PHP installed will be able to interpret the file and display your web page in your browser. Setting up a Node.js server, while not difficult, usually requires more lines of code, and a basic understanding of how closures and callback functions work.

Concurrency :

PHP is synchronous but there are some APIs that behave asynchronously apart from the synchronous lot. It uses multi-threaded blocking I/O to carry out multiple tasks to run parallels alongside each other.

Node.js is asynchronous in nature which means the JavaScript engine runs through the entire code in one go and does not wait for a function to return. It uses event-driven non blocking I/O execution model. The lines of code below the function will execute and the function be executing too and will return the output once done and thus it make Node.js fast.

Modules :

PHP uses module installing technologies like PEAR( framework and distribution system for reusable PHP components.)

Node.js comes bundled with a package management system called NPM (Node Package Manager).

Scalability:

PHP is supported across most popular content management systems (such as Drupal, Joomla, WordPress), which makes it an often choice as a tool for building blogs and e-commerce web applications. In contrast, Node.js efficiently serves as a tool for creating scalable dynamic solutions that deal with numerous I/O operations. It’s also possible to scale Node on multi-cores systems, though with more efforts.

Web Servers :

PHP runs on the Apache web server. It can also run on IIS web server in case of a windows machine. NPM does not need a web server, it runs on its own run time environment.

Performance :

Although Node.js is always highlighted as a high-performative one because of its asynchronous model, PHP has also evolved in this direction. With such libraries as ReactPHP, it has become possible for PHP to be used in event-driven programming as well.

However, when both the environments are compared, you will notice that Nodejs stands out to be a lot faster than PHP, due to following :-

Speed Friendly V8 Engine

Continued Server connection

Callback functions which process many requests at the same time

When Should You Use PHP or Node.js?

Both are back-end technologies, but Node.js can offer an advantage if you’re looking to have a totally JavaScript technology stack across both the front and back ends. If you’re trying to choose between back-end technologies or are building an entire solution stack, it helps to go into a little more detail.

When to use PHP :-

Centralized Server : In case we do not plan on scaling our application across multiple servers we can use LAMP (Linux, Apache, MySQL and PHP) stack. This might change depending on project requirements and growth.

Portability: PHP is a highly portable language. The cheap costs of web hosting and the availability of the servers for PHP is comparable to none. PHP can run on almost any platform that has Apache, IIS and a supported database system installed, this makes PHP applications portable and easy to deploy.

When to use Node.js:-

Same Language across the Stack : Node.js will be the correct choice to use if your project involves software stacks such as MEAN stack ( MongoDB, ExpressJs, AngularJs.), dynamic single page applications, server side technologies and Front end technologies like Angular Js, Backbone.Js or React Js. This makes it easier to have the same language (Javascript) across your whole stack.

Realtime Data: Node.js is great for applications that require real time data, however I would be skeptical about using Node.js for financial applications as Javascript itself is not reliable when it comes to numbers as everything is integer or float and not much separation is done between the types. A more type safe language is recommended when working on financial applications that require lot of computing or a library that is reliable enough.

Speed: Node.js is much faster than PHP when it comes to execution speed, if speed is what you require for your application, such as a browser based multiplayer game or a chat application, Node.js is a great choice than PHP.

Conclusion

Although discussions around Node.js vs PHP don’t seem to cease any soon, the important thing to remember is that there’s nothing unique that you can do only with one of them — they are interchangeable. However, you can always orient at the level of development expertise and stack of technologies that are to be used in the process of development.

Thank you for reading this article, Hope it helps :)

PHP vs. Node.js

PHP vs. Node.js

PHP and Node.js are both used for the server side development and thus have become a competitor for each other.

Originally published at https://www.geeksforgeeks.org

Below are some differences based on different parameters to understand the two and make decision between the two giants. 

PHP VS Node.js

Syntax and Access to command line

Both platforms have access to the command line interface via: 

Example: Printing ‘Hello World’ in PHP and Node.js

The following snippets compare the print ‘Hello World’ program in both the languages:

PHP

// Printing Hello GeeksforGeeks in PHP 
echo 'Hello GeeksForGeeks'; 

Node.js

console.log('Hello GeeksForGeeks'); 

Note: To run the Node.js code, please use the REPL environment.

Synchronous OR Asynchronous

Synchronous code executes line by line and proceeds to execute the next line of code when the current line has been executed.

Asynchronous code executes all the code at the same time.

Note: Program can get stuck in a ‘callback hell’ if a lot of functions needs to be chained which might require piping data from one function to another. However, it can be resolved by Node.js as it has feature of Async/Await which can help a block of code execute synchronously.

CONTEXT SWITCHES

The Switch between different environments and languages is attributed to the drop of efficiency when writing code. Changing between multiple coding languages leads to drop in the efficiency of the programmer. 

MODULES

FRAMEWORKS

Example: Laravel framework

// requires Composer installed on your system
// run following command on terminal.
// This installs laravel on your system
composer global require "laravel/installer"

// Below command creates a folder called
// GeeksForGeeks with laravel installed
laravel new GeeksForGeeks

Example: Express framework web server:

// Below command installs ExpressJS
// in your project folder
npm install express --save

// creating web server using Express framework
// write the following code in your gfg.js file

var express = require('express');
var app = express();
express.listen('3000', function(){
console.log(' GeeksForGeeks demo server
running on express');
});

DATABASES

Negative point PHP: MySQL database systems are especially prone to SQL injection attacks, Cross-side scripting(XSS) and others.

Negative point Node.js: Even though they are not that common, NoSQL injection attacks are a documented vulnerability. But compared to SQL injection, they are negligible. The major reason for this is that they are new and their code design is in such a way that they are inherently resistant to such attacks.

WEB SERVERS

Example: Starting PHP server

// starting php server 
$ php -S localhost:8000 

// index.js file code 
<?php 
echo 'Hello! This is GeeksForGeeks'; 
?> 

The PHP web server was provided to aid application development and can’t be used efficiently as a full-fledged web server.

Example: Starting Node.js server 

// starting Node.js server 
$ node app.js 

// app.js source code&nbsp;
var http&nbsp;
= require('http');&nbsp;

http.createServer(function(req, res) { 
res.writeHead(200, { 'Content-Type' : 'text/plain' }); 
res.end('Hi Programmer\n'); 
}) 
.listen(8080, '127.0.0.1'); 
console.log('GeeksForGeeks Server running at http://127.0.0.1:8080/'); 

Own web servers can be coded in Node.js on which Node.js applications can run. These servers have the potential of high scalability if configured and monitored properly.

APPLICATION DOMAINS

Note: PHP should be used in applications in which client does not have to interact with the server again and again and Node.js should be used for the applications which require a lot of interaction between client and server. 


Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.