jQuery vs. PHP - Performance Comparison

后端 未结 8 710
猫巷女王i
猫巷女王i 2021-01-15 06:37

Performance-wise, which would be the better solution? Here\'s a really small example. The PHP script returns a number to jQuery, which needs to be checked if it\'s 1

相关标签:
8条回答
  • 2021-01-15 06:42

    I'm assuming you have a different case going on, and this is just an example -overly simplified-. The check is a very small one, so I doubt you can measure difference, but lets say you have LOTS of these checks.

    As @mkoryak says, jQuery is clientside and PHP is serverside. If 10^5 users are requesting this, you might see some difference when letting jQuery do this: everyone does it once (and doesn't see the difference), but your server gets to do 10^5 checks less. The other way around, your server is probably a lot quicker then your client, so a lot of calculations for 1 client (with few, or even a single client) might be better run on the server (so PHP would be your choice).

    As @slebetman concludes: For small number of clients, server side code is generally faster. For very large number of clients, offloading work to client side code can greatly improve performance. Here is where @scunliffe 's answer comes in: test your sollution with a stresstest!

    0 讨论(0)
  • 2021-01-15 06:44

    I think you are considering performance over functionality... Let me preface saying that PHP is SERVER-SIDE and jQuery is CLIENT-SIDE. PHP's execution time is dependent on your server architecture and configuration; jQuery is based on your client's computer capabilities. jQuery also does not work in older browsers OR browsers that have javascript disabled. PHP renders standard HTML, so all browsers should display it the same (obviously this is not a discussion about design issues and compatibility).

    0 讨论(0)
  • 2021-01-15 06:46

    jquery runs on the client, php runs on the server. you cant compare the speed of the 2 since one does not even start executing until the other is finished.

    do it all in php

    0 讨论(0)
  • 2021-01-15 06:51

    The correct answer is always - Test/Benchmark it. That way you know for sure which is better.

    That said, if you are doing a basic comparison of PHP (A server-side language) to jQuery (A client-side language) the server-side one should be faster. However it will always depend on what logic you are trying to process.

    0 讨论(0)
  • 2021-01-15 06:54

    Assuming the number is coming from a resource within your OWN website (not some other website's value you're parsing): PHP would be faster.

    • No overhead (done at page execution)
    • Not awaiting page load event to then perform an AJAX query to then parse it out

    If this is a value from another page, I still believe PHP is faster (using cURL/fopen [if site allows it]/etc.). You're talking the difference of something running before the page is fed to the user versus pushing the load on to the user and waiting for their page to load first then populating the field.

    Realistically though, it just depends on how you want the user experience to be. Do you want it seamless or to load as little as possible and build on it as the page gets loaded?

    0 讨论(0)
  • 2021-01-15 07:01

    That would be dependent on your server hardware, but I guess for the overall performance of your site it's better to do such "decorations" on the client side.

    edit: Plus you have less data to submit to the client.

    0 讨论(0)
提交回复
热议问题