performance issues using json_encode and simpleXML

They have: 426 posts

Joined: Feb 2005

I have a php script that makes a call to a web service which returns XML. This is through a URL.

I have converted the xml into json so that I can include a script include tag on the HTML page and use the returning javascript directly.

Are there any performance issues with converting XML to json using PHP?

They have: 5 posts

Joined: Mar 2010

When you're making an extra step there are always performance issues.

I don't really see why you want to encode the xml to json since javascript also can handle xml

They have: 426 posts

Joined: Feb 2005

I realise that JavaScript can handle XML but as I see things, it appears to be more efficient with JSON than XML?

Do you think it would actually be more efficient just handling the XML directly than trying to convert to JSON?

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

It much depends on how efficient the algorith doing the conversion is. If you're looking to get really technical, use Big O notation to work it out.

Personally, I would leave your script how it is, as long as it isn't causing any performance problems.

I would test it though, by feeding the PHP script lots of large XML responses (just create a PHP script that pretends to be like the Web service your script is requesting data from). Use the time() function to see how long it takes to process large amounts of data and whether the server becomes too overloaded.

Frankly, I doubt you'll have any problems. I would also imagine that using JSON would be marginally faster since it is Javascript.

a Padded Cell our articles site!

They have: 121 posts

Joined: Dec 2008

PHP's json conversions have benchmarked' out to be faster than serialize/unserialize - which all the cool kids are using these days... Premature optimization - finding the resource pinch before it exists - is quite difficult. Do the best you can to build that application - optimisation is far easier when you have the complete picture to look at.

That thing you figure may be the resource hog at the start often turns out to be 5% of your execution time while that innocuous process waaayy back there in your code - you know, that simple one - silently eats 40%.

Cheers,
Shaggy.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.