Reputation: 2771
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
, the page needs to say "1 person", else "X persons".
Would it be faster for the PHP script to make this check and return "x persons", or for jQuery to do it after getting the number?
Upvotes: 2
Views: 17520
Reputation: 64409
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!
Upvotes: 11
Reputation: 54445
I don't really think it's a question of speed (although PHP is likely to be faster unless it's running on a very slow server and the client machine is incredibly fast), it's more a question of what's more appropriate.
In this instance, I'd go for PHP as there's really no reason for this change to be made in the browser, once the page (and jQuery, etc.) has loaded.
As a general piece of advice - unless you're attempting to add value to the "default" (i.e.: JavaScript free) user experience by using additional "decorations" (such as slideshows, etc.) that gracefully degrade, do it on the server.)
Upvotes: 3
Reputation: 26997
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).
Upvotes: 0
Reputation: 101604
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.
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?
Upvotes: 0
Reputation: 92752
Doesn't matter, really - the performance bottlenecks are likely to be elsewhere.
I would prefer doing this on the server, as supporting non-JS browsers is important again (what with underpowered mobile devices, web spiders and whatnot).
Upvotes: 0
Reputation: 580
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.
Upvotes: 0
Reputation: 63588
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.
Upvotes: 7
Reputation: 57938
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
Upvotes: 0