2

I have been looking at the node.js application vs php, I found many comparisons comparing these two server technique. Mostly people suggest that the javascript V8 engine is much faster than php in terms of running speed of a single file calculation.

I worked on some javascript code for Node.js, now I have this idea I don't know if it is correct.

In my opinion, Node.js runs a javascript application and listen on a port. so this javascript application is an application that is running on server computer. Therefore, the application code is all copied in the memory of the computer. stuff like global variables are declared and saved at the beginning when node.js execute this program. So any new request come in, the server can use these variables very efficiently.

In php, however, the php program execute *.php file based on request. Therefore, if some request is for www.xx.com/index.php, then the program will execute index.php, and in which, there may be stuff like

require("globalVariables.php");

then, php.exe would go there and declare these variables again. same idea for functions and other objects...

So am I correct in thinking that php may not be a good idea when there are many other libraries that need to be included?

I have searched for the comparison, but nobody have talked about this.

Thanks

2
  • node.js is a socket server at its most basic, so using it is like writing your own webserver, whereas php is writing pages on a webserver. And this isn't the 90s. Any PC that can handle PHP performance wise can handle node.js, and vice versa. And all modern PCs can. Commented Jul 7, 2014 at 23:00
  • I agree that most PC can run both php and nodejs. I am not so much concerned on memory and such, I just had this idea that I do not know if it is correct. Suppose you run a program with like a million grobal variables that need to be declared(yes, i know this is not likely). would php be much slower? Commented Jul 8, 2014 at 4:34

1 Answer 1

2

You are comparing different things. PHP depends on Apache or nginx (for example) to serve scripts, while Node.js is a complete server itself.

That's a big difference, cause when you load a php page, Apache will spawn a thread and run the script there. In Node all requests are served by the Node.js unique thread.

So, php and Node.js are different things, but regarding your concern: yes, you can mantain a global context in Node that will be loaded in memory all the time. On the other hand PHP loads, runs and exits all the time. But that's not the typical use case, Node.js web applications have templates, that have to be loaded and parsed, database calls, files... the real difference is the way Node.js handles heavy tasks: a single thread for javascript, an event queue, and external threads for filesystem, network and all that slow stuff. Traditional servers spawn threads for each connection, that's a very different approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.