SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    json_encode Or jQuery's getJSON

    They both do the same thing, create a JSON object, though one does it on the server end while the other has the browser do all the work.

    For returning output, which is better for performance?

    I guess this is part of a bigger question, when you have an option of processing output data on the server side or the client side, which is better?

    I would imagine for a high traffic website it would be best to push as much of the workload off onto the client side.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,195
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think you have misunderstood what the jQuery.getJSON() method actually does, when you make a request to the server the array you return should already be JSON encoded otherwise your creating a redundant request. If this happens you would be much better off just using the jQuery.get() method which allows you to set a 3rd argument as either (text, html, json) etc...

    With that said i would recommend you always process the array server side all the time as JSON encoding an array barely uses any memory, no matter how small the array is though i will always process the array server side to avoid creating redundant code.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification, and for the informative response to both my questions.

    Unless I'm wrong again, it sounds like these two functions are likely both to be used in a single HTTP request and response.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,195
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yes, as your echoing the array back JSON encode it at the same time, see the below example:

    PHP Code:
    $myArray = array('hello''there');

    exit(
    json_encode($myArray)); 


Bookmarks

Posting Permissions

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