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 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?