March 4, 2012

$.getJSON is slow

Question by Abhishek Kumar

I am using $.getJSON to pull inventory of objects (100 items, not a large set) but its taking 8-10 seconds for XHR call.

Would like to learn if there is something I am missing or something I could do to expedite my program?

Answer by Starx

There are many things involved on it. Two main bottleneck I think are these

  • The server script generating the JSON might be causing the problem or taking long process time to generate the output.

  • You have too many scripts on your page like effect or slider, that the getJSON, is running slow

In most of the cases, the first one is a silent killer. In order to test your script, make a getJSON request to page, which simply displays the JSON string, without any processing. Something very lightweight like this

{ "test" : "value" }

In PHP

echo ‘ { “test” : “value” }’;

And make a getJSON request to this page and check if the script is running fast. In case it is fast, the problem is in your server script.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!