Is a -dynamic- PHP app more performant than a NodeJS one?

  event-loop, io, node.js, nodejs-server, php

I’m new to NodeJS, coming from mostly PHP, and am building an app that requires a fresh HTML serve from the server on each request – Node serves HTML with the EJS templating engine – no SPA framework. I’ve stumbled upon a case when I’m not sure if PHP is most suitable or Node, due to server I/O operations.

My app’s entire request process is fully asynchronous. All DB (Mongo) calls, readFiles, EJS renders and includes, etc. The point is that all code that can be async, is async, to not block the event loop and server I/O operations.

However, my application needs to serve and render a ton of user-specific, dynamic data, and the rendering has to be synchronous. The application is a customer management system with many pages and options, that at any given point will have several dozen logged-in users at once, that each needs to have data that is related to them (so fully caching the HTML isn’t really an option).

For example, if I get a list of 1,000 items from the database and need to run a regular for loop to render it (using EJS or without). A for loop, as far as my understanding goes, is fully blocking. Even if it’s very fast and simple to handle 1,000 items, it’s still blocking. This is evidently problematic if there’s really a lot of rendering to do with simple logic like a for loop blocking the server.

Here’s a piece of code to illustrate what I’m talking about:

const arr = new Array(1000);
let ul = '<ul>';

for(var i = 0; i < arr.length; i++){
    ul += `<li>Item #${i}</li>`;
}

ul += '</ul>
res.end(ul);

The simple example above, of rendering a simple UL, as fast or slow as it is (depends on the data that’ll be in the array) will block the entire server for all users.

I guess it’s the same concern with an eCommerce app with many users requesting pages simultaneously. Will such an application be more performant with PHP rather than Node because running simple logic with PHP, doesn’t matter how large (mostly) the data set is, just doesn’t block the server from serving other users? Or perhaps I’m missing something in regards to NodeJS? how would you handle such a case in a production environment with NodeJS (if at all)?

Source: Ask PHP

LEAVE A COMMENT