Entries Tagged 'PHP' ↓

JSON versus XML

There has been much debate on using XML or JSON with your AJAX, but in my mind, the debate is pointless. Both are nice, one is quicker, one is slower. However, when it comes to development pick what you know best and what you can implement/maintain easiest.

If you do this, you are guaranteed the following:

  1. It is easy to maintain and understand the code you wrote months later
  2. Chances are your application will not be the quickest in response
  3. You will make use of techniques you fully understand than trying to adapt ones that you are unfamiliar with and may implement wrong

So how does JSON differ from returning XML in your AJAX requests? Lets first look at the advantages of XML.

  1. Standardized and easily serialized into via .NET
  2. The XML is easy to read all within itself
  3. You can make use of XSLT, DTDs, etc

How about disadvantages of XML?

  1. It is slow in comparison to JSON
  2. It can be clumsy when not designed properly

Now for the advantages of JSON:

  1. It is faster than XML to parse in JavaScript
  2. There are addin’s you can install in .NET or a library in PHP to serialize a JSON string from an object
  3. It is hard to make this clumsy as it is based on the Object Design

And the disadvantages of JSON:

  1. It is harder to read the serialized output
  2. You can’t use XSLT, DTDs, or something of the sort to validate or style the request

As for myself, I am a JSON person. I didn’t used to be though. My original AJAX applications were focused around XML primarily because I could make use of an XSLT and I always validated against a schema. However, JSON has won me over due to speed. JSON’s speed is just ridiculously fast and it showed when I converted a major application over to it. I saved nearly a third of the time it took to do a fairly simple task.

However, pick the poison that you understand most, otherwise, it could have detrimental affects.

Refactoring – What a time-saver

This weekend has been a nice relaxing weekend for me and so when I got back home this afternoon (on Sunday the 16th), I was in a great mood to refactor some of my kiosk code base. Now, I should start by saying, I decided to refactor code instead of write new code tonight because I wanted to lessen the number of files that needed to load in the Ajax functionality.

Kiosk, uses Ajax in interesting ways to provide a better experience, but with that, the Ajax applications were built as each page was assembled. This caused functionality across pages to get a bit messy or functionality shared across pages to get thrown into individual files. Well the problem with individual files, is that is yet another file for the browser to download.

So before I get any further, let me discuss refactoring and how it can solve many performance binds your application may be experiencing.

To start off, refactoring is taking common code, or code that is used across pages or frequently and moving it to a more global location so it can be called easier and the code only has to be written once.

So where can this help?

  • First of all, by refactoring code into a single global file, you can limit the number of HTTP requests if that file is accessible via the web. Since your common code is in a single file, the browser only has to fetch that code once, and will then likely cache that file for each page after, giving you even better performance.
  • Secondly, by placing the common code in a global method/function, you are writing less code. As now when you need to call that code on your second page, it is already written, so just call the function. Otherwise, you would need to write the code all over again.
  • Thirdly, less code equals smaller files. By refactoring your code, you can have smaller file sizes and in turn that is quicker to download, quicker to parse, and quicker to execute.

Now in my situation, I took about 4 JavaScript files and combined them into a single file adequately named “general.js”. The file contained code to automatically log the user out after a set amount of inactivity in the UI (User Interface). Secondly, it contained the version checking mechanism calls that see if they are running the latest version of the software. Unfortunately, those are the only two things it does right now, but as the project progresses, I am sure more utilities will get added in this file.

My next task is to refactor the Data Grid functionality so it is 1) XHTML, 2) easy to build complex columns with complex rows, 3) customizable by identifying StyleSheet classes for the columns and rows. Hopefully, I can get my head around this idea and get it working with little trouble to the programmer.

So what can you think of that you need to refactor? What performance gains do you suspect you will achieve with the refactoring you do? Any performance markers?

Ajax versus Socket Connections

Ever have to argue Ajax versus Socket Connections? Me either. Today, I had the chance to talk about the differences, their advantages and their disadvantages.

Ajax has many advantages. You do not have to parse content/page headers, you can parse XML, you can build objects out of JSON serializations, and above all there are a lot of frameworks that are cross-browser friendly so you do not have to worry about writing the framework yourself.

A Socket Connections’ advantages are much smaller in my opinion. There is no browser limitations, which is a major advantage, keeping it browser independent. It can support any type of response content, JSON serializations, XML, HTML, strings, etc.

So what about the disadvantages? Ajax has the complications of cross-browser friendliness, but that has been removed by frameworks doing all that hard work for us. Ajax’s biggest disadvantage is probably the lack of JavaScript standards being followed by each browser. Though this is removed by using popular frameworks, it matters for the rest of your JavaScript code to setup the Ajax event.

So how about Socket Connections? Socket Connection have the complication of trying to figure out where the response data starts and where the response headers end. Secondly, they are language dependent. Not all languages support sockets and some may make it very complicated to use them though, whereas, Ajax is getting easier and easier to use.

What are your thoughts?