views:

270

answers:

5

i have an chat application written in jsp and jQuery. I have used the setTimeout method for threads who are continuously posting request in some URL. My browser is hanging after some time and cpu utilization is very high.

Is there any way to increase performance of JS?

+5  A: 

Check this article.

Daniel Moura
It advices to avoid selectors and DOM manipulation. But then why would I use jQuery??
hasen j
@hasen j: You're misrepresenting the article.
Chris Farmer
+1  A: 

Check this one: http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip1

It has some really good recommendations like:

  • Keep selection operations to a minimum by caching
  • Keep DOM manipulation to a minimum
  • Use IDs instead of classes wherever possible
  • Give your selectors a context
Santi
A: 

Have you tried debugging with Firebug? You can use it to debug javascript, monitor requests and responses, and more.

jrummell
A: 

Well i run trough this before here is what i did (by the way this has nothing to do with jquery) :

try {
    window.clearInterval(x);
} catch(e) {
    // do nothing
}
x = setInterval(Update, 1000);

in your case you should clearTimeout :)

try this :

refresh() { $.post("./chatServlet",  {  message: "hxci",  name: $("#author").val(),  event: "AUTOROOMMESSAGE",   roomname: $("#select").val() },   function(xml)   {   $("#msg").empty();   addMessages(xml);    });    try {    window.clearInterval(x);}    catch(e) {       }     x = setInterval(refresh, 3000);   }
Yassir
Since we are talking about optimization here, I should mention that there is no reason to check x for null before calling clearInterval. That's just an additional cycle that we do not need!
Josh Stodola
Josh Stodola : The first time this will run the interval will be null and if you call window.clearInterval(x) and x is null ... and really do you think checking for null is a perf penalty ?
Yassir
Why whould you clear the interval and set it new? Either you should always use `setTimeout()` or just let the interval run.
Georg
Well i had a recursive call to "update" function Update() {//magic here try { //window.clearInterval(x); } catch (e) { // do nothing } x = setInterval(Update, 1000); }the browser keeps taking more and more memory until it blocks the system but when i cleared the interval everything went back to normal !
Yassir
hi Yassirfollowing is the my functionfunction refresh() { $.post("./chatServlet", { message: "hxci", name: $("#author").val(), event: "AUTOROOMMESSAGE", roomname: $("#select").val() }, function(xml) { $("#msg").empty(); addMessages(xml); }); setTimeout('refresh()',3000); }i have 2 recursive func in my code like this.browser is taking high memory at client end.
pradeep
A: 

I think the problem are to often called setTimeout(). Javascript isn't multithreaded and therefore the browser cannot run any other code as long as a timeout-function is running.

Georg
"timeout blocks the browser" ... huh???
hasen j
@hasen: The Javascript engine.
Georg