Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.

View Poll Results: Read my message before voting

Voters
5. You may not vote on this poll
  • PHP

    4 80.00%
  • JS

    1 20.00%
  • Depends on the users connection

    0 0%
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    114
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Which is faster - PHP or Javascript

    I know this is comparing apples and oranges, one is client side and one is server side. But say I am making a randomizing script. I can do this in either PHP or Javascript, and the first time through I chose JS. I am redoing things now, and need to know is one better than the other (speed wise)

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP, because the user might not have JS installed or enabled. it's the kind of script which is somewhat superfluous and is therefore best done serverside.
    *keep it simple (TM)

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    114
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Thank you, I will go and do it in PHP then .

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation General thoughts

    For this kind of script I'd definately go server side as well.

    But in general, i'd try to use both for certain types of scripts. But never base my core functionality on Javascript, hench scripting being disabled on some machines.

    Majority of machines have Javascript enabled, so catering for them, should give you better performance in certain cases - but still base core functionality server side.

    But ya, this all is obvious, just stating the obvious hehehe.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If you need speed, in theory any server-side language is faster than javascript. This advance reduces considerable in case of repeatedely submits/change of sessions, as user have to wait a wile the server response.

    In fact there are few things where php (or any server-side language) and javascript chalanged, for the simple fact that they were designed for different purposes. Usually, a developer uses succesfully both languages.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by jbot
    PHP, because the user might not have JS installed or enabled. it's the kind of script which is somewhat superfluous and is therefore best done serverside.
    Personally, I'd consider 'superfluous' scripts best handled client-side where possible. Why load the server with tasks which aren't critical to the site?

    (However, depending on the criticality of the random effect, I'd still be inclined to go server-side.)

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mmm

    See, if i look at the whole postback structure thing they've got going on in asp.net for example, lets think about the Calendar for example.

    Each time you click on a date item, a postback occurs.... which in itself is a waste, a lot of components does this, cause they rely on the viewstates, which requires postbacks.

    Lot of this can however be done client side, and simply be submit, when the user is done with the current page - They are using a lot of javascript in there already in anycase for their components, why waste it on going up and down to the server all the time?

  • #8
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mmm?

    Quote Originally Posted by Kor
    If you need speed, in theory any server-side language is faster than javascript.
    Interesting comment, i doubt one can really compare client side scripts and server side scripts with each other....

    If you want to compare it looking at the postback issue doing simple tasks, javascript wins, since you can do 1 postback once you're done, instead of doing 1000 for simple tasks.

    But other than that.... cant really compare them.

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Actually, in theory, putting off more processing onto the client is more efficient. In reality, however, this is not the case.

    The theory is that if you have 10,000 tasks, for example serving 2 page loads to 5,000 clients, then what's more efficient, processing 100% of 10,000 tasks on one centralized server (could be a farm, granted) or processing 5% of 10,000 tasks on the centralized server and 95% of 10,000 tasks on 5,000 machines (the clients). In theory, passing the processing off to the client is more efficient.

    But the reality is that browsers are slow. None are designed with dynamic content in mind as the most important thing. The most effort on speed goes into rendering a static page, because that's the staple of the internet. The reality is that the clients are almost exponential in the extra time they take to process logic for dynamic page rendering when compared to preprocessing it on the server.

    So, for many many things, at this point in time server-side processing is simply more efficient, barring all discussion of security and consistency for the moment. But there are many ways to use the client properly, that use the clients' strengths and mitigate their weaknesses to increase the efficiency of the system over all. Your example of ASP controls constantly posting back is an example of bad system design, not a question of which is faster server or client. If your server is only interested in certain pieces of information, and multiple steps are required to collect that information, figuring out a way to collect it all and preprocess on the client efficiently is always better than making multiple round trips to the server for it. And that's because the theory is sound, and it can be executed properly sometimes. It IS always more efficient in theory to off load processing to the clients. Our job as programmers is to determine when the theory and the reality line up, and when they do not.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    This question is like asking whether a car or a plane is the better form of transport. You wouldn't use a plane to visit the next su8burb and you wouldn't use a car to travel half way around the world.

    Similarly if you are talking client side processing Javascript is much faster since PHP doesn't run at all. For server side processing PHP is much faster since Javascript doesn't run at all. For Ajax processing you need both and trying to use just one or the other wont work so neither is faster there either.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    The title of the thread and question in the poll are definitely meaningless as you state, but the OP's post clears up his question. He's trying to figure out what's faster, doing things client side or doing things server-side.

    I think his question can be stated easily this way:

    "Barring security and data integrity, is it faster to do logical processing on the server or the client?"

    It just seems that the OP uses JavaScript to mean "client-side processing" and PHP to mean "server-side processing". I think it's clear from his posts that he is using his terms loosely.

    I hope that this thread not only clears up the various options, pros, and cons for people, but also helps them learn to speak clearly, and differentiate their terms properly so they can participate more effectively in technical discussions such as these.

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Doing things client side is always faster because it avoids the round trip to the server. Doing things server side is always needed as at least some of your visitors will not be able to run the client side processing and some things can't be done client side.

    The best way to handle this is to create your pages using server side processing so that they work that way then add javascript to speed up the processing where the trip back to the server can be avoided but leave the server side version untouched so that it still works without Javascript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •