duangai2831 2018-01-30 08:15
浏览 86
已采纳

PHP vs JS:为什么PHP操作基本上是同步执行而JS操作不是?

If we look into the way PHP and JS frameworks are written you can see a great difference. JavaScript generally has callbacks/promises for operations that might take an undetermined amount of time. Even though PHP has the possibility to pass functions as a parameter, it's not commonly used.

I'd like to take database operations as an example to state my case:

PHP's Eloquent:

$organization = Organization::where('name', '=', 'test')->first();

Node.JS Sequelize:

Organization.findOne({where: {name: 'test'}})
    .then(function (organization) {
            // Organization has been fetched here
        }
    );

As you can see Sequelize uses promises to make the operation async. However, PHP's Eloquent does not.

Apart from framework benchmarks, does this mean the Node.JS is faster/more efficient since it is not blocking? Or is this rather because JS is single-threaded? Maybe I have a wrong perception and is the pattern equally implemented in both languages?

  • 写回答

1条回答 默认 最新

  • duanqinqian5299 2018-01-30 08:31
    关注

    It's possible to write blocking node.js code, and it's possible to write asynchronous PHP code (see the libevent extension, the amphp project or the ReactPHP project). Neither is really idiomatic. This means it's atypical and perhaps not recommended except for specialized use-cases.

    PHP can work synchronously because in a typical webserver there are many PHP process running at the same time. Each handling 1 HTTP request at a time.

    There are pros and cons to each model. For example, it's harder to do multiple things in node.js if you're doing CPU-bound work, and in PHP you might need more memory usage for higher concurrency, because often you might be waiting on IO in PHP, so most of your PHP processes might end up waiting. So your CPU usage ends up being low, and you have a lot of PHP process waiting around for IO while all of them have a copy of your favourite web framework in memory.

    Node.js allows for building some types of applications that are harder to do in PHP because there's no shared memory space. But then again, that advantage might end up being less relevant once your node.js application needs to grow beyond a single process.

    Anyway, it's an extremely broad question. In some ways both node.js and PHP can address similar use-cases, but when your use-cases become more exotic one of the two might be better.

    If you're mostly a building a web application, you're not expecting massive scale, it probably doesn't really matter that much what you go for.

    So ultimately which is the better choice depends on your use-case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)