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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效