# AJAX slow - to\_json responsible?

**URL:** <https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832>\
**Category:** Rails\
**Created:** [March 13, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832 "2008-03-13T17:51:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 13, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832/1 "2008-03-13T17:51:05Z")

</div>

Hello.

I am building full-ajax back-end, which consits of many quite big  
partials. As the system grows up, performance is problem. After every  
click, partial is refreshed, if needed. I am now at 0.5 sec / request. I  
tried to simply disable ajax and the result was quite suprising: the  
same page is rendered over 2x faster. I update 80% of page in case of  
ajax. (Please dont tell: dont use ajax when updating 80% of page…  
thats not the point.) So the ajax (page.replace :partial=\>) is  
significantly slower than non-ajax? Are there ways to improve  
performance? I’am not very deep in rails, some suggestions?

Thanks. Fero.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 13, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832/2 "2008-03-13T17:58:15Z")

</div>

On 13 Mar 2008, at 16:51, Frantisek P. wrote:

> thats not the point.) So the ajax (page.replace :partial=\>) is  
> significantly slower than non-ajax? Are there ways to improve  
> performance? I’am not very deep in rails, some suggestions?

Well the first step is to workout where the bottleneck is (via ruby-  
prof for example, there’s a rails plugin that’s easy to use).  
Beyond that, it is certainly true that page.replace :partial=\> is  
slower than just a plain old render, since on top of the normal  
render, the result of the render must be turned into a javascript  
string (escaping quotes in it for example), and then on the other end  
the browser does the exact opposite. I remember finding it a big  
sluggish with large chunks of html.

Fred

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 13, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832/3 "2008-03-13T19:41:04Z")

</div>

I actualy found that cca half of request is in String#gsub, in 90%  
called by String#to\_json. Any idea how I should improve performance?

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 13, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832/4 "2008-03-13T19:59:26Z")

</div>

Frantisek P. wrote:

> I actualy found that cca half of request is in String#gsub, in 90%  
> called by String#to\_json. Any idea how I should improve performance?

maybe by using [http://json.rubyforge.org/](http://json.rubyforge.org/) c-variant of String#to\_json

But, how can I do that?

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 13, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/ajax-slow-to-json-responsible/131832/5 "2008-03-13T20:16:17Z")

</div>

Ok, my first performance-tunning success:

Using [http://json.rubyforge.org/](http://json.rubyforge.org/) the time calling String#to\_json drops  
to 4%, which improves overall rendering performance by 30%.

Thank you for attention 🙂 F.
