# Performance of to\_xml, versus to\_json

**URL:** <https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108>\
**Category:** Rails\
**Created:** [May 1, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108 "2007-05-01T22:45:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Mill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Eric\_Mill](https://www.ruby-forum.com/u/Eric_Mill)\
**Post date:** [May 1, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108/1 "2007-05-01T22:45:55Z")

</div>

I’ve been doing some benchmarks on how fast ActiveRecord’s #to\_xml is  
on large datasets, versus the speed of A  
ActiveRecord’s #to\_json, and the difference is rather alarming.  
#to\_xml becomes quickly unusable for large datasets, while to\_json  
remains tolerable.

The important thing here isn’t how fast #to\_json is – it’s that you  
can’t practically use #to\_xml anymore after a certain point, a low  
point.

Some basic benchmarks, with a User model that has 5 fields: an  
integer, a string, a text, and two datetimes.

Commands benchmarked:  
to\_xml - “User.find(:all).to\_xml”  
to\_json - “User.find(:all).to\_json”

50 items:  
to\_xml - ~0.20 seconds  
to\_json - ~0.02 seconds

500 items:  
to\_xml - ~6.9 seconds  
to\_json - ~0.4 seconds

1000 items:  
to\_xml - ~25 seconds  
to\_json - ~0.9 seconds

8000 items:  
to\_xml - \>10 minutes (didn’t wait for it finish)  
to\_json - 10.7 seconds

Can anybody comment on this? With Rails pushing ActiveResource and  
REST like crazy, services which really start allowing people to list  
their products and userbases in XML are going to have a tough time  
with out-of-the-box Rails, with performance like this.

– Eric

---

<div class="post-metadata">

**Author:** ![Eric\_Mill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Eric\_Mill](https://www.ruby-forum.com/u/Eric_Mill)\
**Post date:** [May 2, 2007, 2:46am UTC](https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108/2 "2007-05-02T02:46:18Z")

</div>

> Can anybody comment on this? With Rails pushing ActiveResource and  
> REST like crazy, services which really start allowing people to list  
> their products and userbases in XML are going to have a tough time  
> with out-of-the-box Rails, with performance like this.

From your numbers, this isn’t an issue for the vast majority of  
services. I’m not sure why anyone would send 8000 items in a request.  
But, if this concerns you, I’d be happy to commit some performance  
patches for it.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![Eric\_Mill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Eric\_Mill](https://www.ruby-forum.com/u/Eric_Mill)\
**Post date:** [May 2, 2007, 3:41am UTC](https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108/3 "2007-05-02T03:41:22Z")

</div>

> From your numbers, this isn’t an issue for the vast majority of  
> services. I’m not sure why anyone would send 8000 items in a request.  
> But, if this concerns you, I’d be happy to commit some performance  
> patches for it.

Hi Rick. Yeah, Eric’s 8000 example is the extreme case, but any  
performance patches would be great, and we’re willing to help in any  
way we can.

I was also wondering what to\_json wasn’t doing (besides the  
obvious :include stuff) that it should be doing to bring it on par  
with the to\_xml code. We’ll likely switch to json for our specific  
application, but it’d be good to know any issues before we dive in  
(granted, we’re more than willing to fix any issues we find once we  
get in there).

thanks,  
-Chad

---

<div class="post-metadata">

**Author:** ![Eric\_Mill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Eric\_Mill](https://www.ruby-forum.com/u/Eric_Mill)\
**Post date:** [May 2, 2007, 3:54am UTC](https://www.ruby-forum.com/t/performance-of-to-xml-versus-to-json/103108/4 "2007-05-02T03:54:22Z")

</div>

On 5/1/07, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was also wondering what to\_json wasn’t doing (besides the  
> obvious :include stuff) that it should be doing to bring it on par  
> with the to\_xml code. We’ll likely switch to json for our specific  
> application, but it’d be good to know any issues before we dive in  
> (granted, we’re more than willing to fix any issues we find once we  
> get in there).

Eric’s email sounded kind of like he was volunteering. And, I am a  
very willing receiver of such patches 🙂

I’ve done a bit of json hacking using the fjson gem:  
[http://svn.techno-weenie.net/projects/plugins/json\_for\_rails/](http://svn.techno-weenie.net/projects/plugins/json_for_rails/)

The big problem I ran into was worrying about dates and times. My  
plugin converts them all to xmlschema times. But, there’s no way to  
tell the type of incoming objects, so I used a regex to cast xmlschema  
times back to Time objects. This could cause problems if someone  
posted an xmlschema time as the body of a post though. Perhaps a  
better solution is to assume _\_at/_\_on fields are times? I’m not  
sure. I’d like to stay consistent with other json API’s, but from  
what I gathered they differed anyway. So, I shelved the plugin for  
now.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
