node. js and PHP operating mechanism comparison

Source: Internet
Author: User

Why is it advantageous to use node. js? A new technology is popular then it must have its advantages, then let's simply compare it to PHP

1. node. js he uses the JavaScript engine, so it's destined to be a single thread, using asynchronous methods to open up multiple tasks without having to wait for the next use of the last task thread, like PHP.

PHP is also single threaded but it borrows from Apache servers to provide multithreaded services

2. High concurrency, large data volume How to deal with:PHP: Optimized SQL, with components, with caching, in order for the thread to end as soon as possible, the next task node: single-threaded, asynchronous, event-driven here's how they handle the event,PHP up and down in order to execute, node because it is running fast and will not wait, so if you use the result of the previous return, you need to wrap the back, as a callback function execution node. js vs phpAdvantages: High performance (operational mechanism issues) high development efficiency (save a lot of optimization things) wide range of applications (can develop desktop systems, electron framework) shortcomings: New, less middleware, less IDE imperfect node. js's weaknesses and solutions1 The default does not support multi-core, but can be resolved with Cluster 2 server cluster is not supported by default, Node-http-proxy can solve 3 using Nginx to do load balancing, static by Nginx processing, dynamic with node. JS Processing 4 Forever or Node-cluster for disaster recovery here is a data test request--randomly create a document that is 20k characters--Read the document--output The result is that node takes much less time than PHP takes. 1 Node   
var http = require (' http '); Http.createserver (function handler (req, res) {Res.writehead ($, {' Content-type ': ' text/html; charset=u    Tf-8 '});   if (req.url!== '/favicon.ico ') {str = ""; Random characters-20k//randomly generated file filename = string.fromcharcode (math. Floor (Math. Random () * (122-65)))        ) + ". txt"; STR assignment for (i = 0; i < 200000; i++) {n = Math. Floor (Math. Random () * (122-65)))            ; str + = String.         fromCharCode (n); }//write var fs = require (' FS ');//manipulate file module//write Content Fs.writefile (filename,str,function (Err, FD) {if (err) throw err;//If the error throws an error//reads the file and displays the page Fs.readfile (fi                     Lename, function (err, data) {if (err) throw err; Res.write (data);//Output Res.end (");                End}); }         ); }}). Listen (8000); console. Log (' success:8000 ');

Php

  

1 <?php 2  3 $str = "";   Random string 4//Text name 5 $fileName   = chr (rand (0, 57) + 65). TXT '; 6  7 for ($i = 0; $i < 200000; $i + +) {8  9      $n = rand (0,) +      $str   =   $str. Ch R ($n); 11}12 13//write to File_put_contents ($fileName, $str); $result = file_get_contents ($fileName); 18 1 9 echo $result;?>

node. js and PHP operating mechanism comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.