WANTED!! YOUR Opinion: best to use in AJAX Response.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply

What is your ideal output format for AJAX response

XML Doc
0
No votes
HTML Snip.
2
50%
JSON Str.
2
50%
 
Total votes: 4

matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

WANTED!! YOUR Opinion: best to use in AJAX Response.

Post by matt1019 »

Hi Guys,

First, some background information for people new to AJAX, or just not familiar with the subject/concept. (This would be not needed for 98% of people on this forum since most of us have used some sort of Javascript in combination with other scripting languages etc.... but dosen't hurt to provide background information on the topic.)

Recently, I stumbled upon this page on the web (through google search)

http://www.quirksmode.org/blog/archives ... espon.html

can't tell you how much this has helped me!!

Now comes the participation part Yay!! ;)

I would like to ask, what do YOU use for Ajax response? XML HTML or JSON?

If you used any of the three in the past, and now switched to different method, why?

on a side note:
Some people (I consider myself in this group) do not know what they are talking about when they say "AJAX this" "AJAX that" blah blah blah.
Some of the time, when we say AJAX, we actually are looking for AJAH functionality.

Ok, so back to the topic, sorry for the digression. So, what kind of format do you respond with when utilizing AJAX or AJAH.

As for me, I have rarely dealt with JSON... actually only once:
ole was helping me in one of the features that I wanted to add to my project....

On a regular basis, I now deal with HTML only. So, in a way this thread will help me shape my ideas. (I am leaning towards using JSON strings in the future though)

Why?
JavaScript does not allow you to access documents (be they XML or HTML) that come from another server. However, if you import a JSON file as a script tag you circumvent this problem, and any JSON data can be imported into any website.
quoted from the aforementioned site ;)

-Matt
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

JSON are javascript objects expressed using literal notation..,meaing their native to javascript...

Meaning, you can easily use JSON to quickly extract information...

XML on the other hand, is more...universal I would say...

So it depends who would use that ajax service - if you will.

Lets assume, you have a script which scans Google and parses the HTML and returns the top 4 listings for X number of keywords...

Your buddy calls this script using AJAX on his web site because he doesn't have PHP enabled hosting...so he relies specifically on client side javascript.

To him it doesn't matter, as javascript can work with both JSON and XML although it's very trivial when data is returned as JSON whereas XML would require using the DOM...

However, if another friend wishes to call the original script using Perl or PHP, they might have more problems as using JSON would likely require finding a PHP JSON library...which admittedly wouldn't be difficult to write or find out (I would guess) is sitll a burden, whereas XML would also require using SAX or DOM...which opens up a whole different can of worms...

So either in this case is toublesome, except when returning JSON to known client side callee's as JSON is native to javascript...

Capeesh?

Then there is a matter of how *much* your script will return...if the amount of data returned is in the MB's it's probably really wise *not* to use JSON as an object that big would likely crash your client computer for sure and possible leave PHP hanging for a moment...

XML has the distinct advantage of being able to be useful, via SAX but the DOM would be a mega memory hog...

Really the return format depends on a few variables...the above are some mentioned...

Depends on the kind of data being returned...if it's tabular data only...CSV would suffice...

p.s-Thanks for making me think about this...I've never bothered before...this was a good excersize... :P

Cheers :)
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

JSON, unless you have a specific need for XML other than just a data transport.

Using JSON has a huge performance advantage over XML. XML has to be parsed and then access through the DOM. You can just eval() JSON, and then you have access to a completely native object. JSON is a lighter format and will result in less data being transferred between the client and server.

As far as sending HTML, it seems like it would be bad practice, and is just a workaround for the one specific problem mentioned in your post. If for some reason you need that functionality, I suppose you might do it this way.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

bg wrote:JSON, unless you have a specific need for XML other than just a data transport.

Using JSON has a huge performance advantage over XML. XML has to be parsed and then access through the DOM. You can just eval() JSON, and then you have access to a completely native object. JSON is a lighter format and will result in less data being transferred between the client and server.

As far as sending HTML, it seems like it would be bad practice, and is just a workaround for the one specific problem mentioned in your post. If for some reason you need that functionality, I suppose you might do it this way.
JSON would quickly get ugly I think for medium-large amounts of structured data...

JSON needs to be parsed as well...when eval is called...it'll parse the object...

I'm willing to bet a XML parser is much faster than a scripting language...

Sending HTML would only make sense if what the AJAX service extracted or output was going to be directly dumped to screen...in which case no further processing is required...

Each has it's up and downs...it really depends on the situation
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

You will not find a single instance where parsing xml is faster than parsing JSON (in javascript), when the xml and json is identically structured.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

bg wrote:You will not find a single instance where parsing xml is faster than parsing JSON (in javascript), when the xml and json is identically structured.
Ok then... :P

i see you've put your foot down on this one...just curious but have you profiled it? I haven't but I'd be interested in seeing the difference...

Anyways, I would argue that *parsing* would not be faster...perhaps the operation on a whole might be...but parsing...I dunno about that one...
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Hockey wrote:Each has it's up and downs...it really depends on the situation
Exactly my thoughts. I use JSON when I'm retrieving a person's data (height, weight, name, age, sex, eye colour, etc...) since I'll likely be using bits of that data for different purposes.

On the other hand, if all I was retrieving was the status of a username (on a registration page, whether it is taken or not) I would use HTML, as I really just need to display the result "<div id='usernameresult'>Username Taken!</div>"

The JSON library I use (forgot the name atm) is fairly fast, at least with the array's i've been throwing it. Which is the other bonus of JSON when used for ajax/php, just simply use JSON::encode($UserDataArray); and javascript JSON.decode(ReturnResponse) on the client side, transfering complex, nested data structures is very easy.

So... It depends ;)
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Hello Guys,

Thanks for the replies. Your comments helped me understand the topic even further. Thanks once again.
So either in this case is toublesome, except when returning JSON to known client side callee's as JSON is native to javascript...

Capeesh?
Capishun (hope I spelled it correctly ;))
You will not find a single instance where parsing xml is faster than parsing JSON (in javascript), when the xml and json is identically structured.
interesting note. I will jot that down.

Thanks again guys, :)

-Matt
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Capishun (hope I spelled it correctly ;))
Me too...as in I hope I spelled mine correctly as well :P
interesting note. I will jot that down.
Until someone profilines that...I woudl remain weary of that claim...as technically...i'm not sure it would make sense...

XML parsers are typically blindingly fast...XML is a fairly simple language whereas a programming language is quite the contrary...

Look at how many XML parser are available compared to javascript or other source parsers...

The latter is quite a complex subject, often limited to a esoteric few...those who have degrees in computer science or study compiler design...

I've been interested in compiler design for many moons...and I can honestly say it's about as complex as computer science can get, outside of AI, which is hard to understand because of the abstract thought involved, whereas compiler design is centered more around natural language parsing...albeit a little different...

The point is...if the JSON objet is indeed faster, it'll likely only be so in small objects with limited data...the way most scripting languages implement objects make their extensive use significantly decrease system performance...which is already outside the scope of parsing...but still...in both cases...XML IMHO would win...

Deeply nested objects wouldn't parse that quickly and when they did, they would probably consume more memory than even a DOM...

Of course, no one knows until you actually profile those sections...which would be difficult to do...

You would have to determine the COM interfaces (in IE) for both xml parser and javascript engine...which admittedly wouldn't hard...but hooking into their events would be an entirely different challenge...

Of course you could just use Mozilla and it's open source, but finding the right spots to profile would be next to impossible for anyone but core developers or someone who had an insight into how that system of code works :P

Cheers :)
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

http://www.crossedconnections.org/w/ind ... ce-update/

I was wrong, JSON is only marginally faster in most cases. But what's really interesting, is IE blowing away firefox and opera in parsing both json and xml. Holy <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Those benchmarks say nothing...the amount or type of data isn't indicated...??? :?

Code: Select all

var obj = {
  First_name : "Alex",
  Last_name : "Somehting",
  Address : "Somewhere in Canada",
  Postal : "R2R 2R2",
  Phone : "123-4567"
};

Code: Select all

<person>
  <firstname>Alex</firstname>
  <lastname>Something</lastname>
  <address>Something</address>
  <postal>Something</postal>
  <phone>Something</phone>
</person>
On data like the above JSON may likely be moderately faster...not in parsing as I still say: XML parsing would win...but the DOM likely takes more to initialize than a Javascript object...

Try it with deeply nested objects:

Code: Select all

  <species>
    <bipedal>
      <homosapien>
         <male>
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
            <profile name="Alex" age="27" height="5.75" />
         </male>
         <female>
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
            <profile name="Kristina" age="27" height="5.75" />
         </female>
      </homosapien>
    </bipedal>
  </species>

Code: Select all

var species = {
  bipedal: {
    homosapien: {
      male: {
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Alex",
          age: "27",
          height: "5.75",
        }
      },
      female: {
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        },
        profile: {
          name: "Kristina",
          age: "27",
          height: "5.75",
        }
      }
    }
  }
};
Use the above test data to conduct your profile on...still won't be *that* accurate...but atleast I know what your going against...

There are 25 profiles under each gender, 50 in each JSON and XML...

Post or PM me the results...I'm seriously curious :)

Cheers :)
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

There is a link on that page, where you can run the test in your browser. You can also view the source to see the data used.

http://www.crossedconnections.org/w/wp- ... ntest.html
Post Reply