Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slow Javascript execution? 1

Status
Not open for further replies.

Layton

Programmer
Nov 29, 2001
15
US
I hope you can help me. I have a problem with Javascript running slow in an ASP page.

I inherited this program from a programmer who is no longer employed by the company. One of my first assignments is to speed up the program. Upon checking one of the slow pages I found that it was taking over 15 seconds to open a specific page.

While trying to find the problem I discovered that if I inserted a “response.write” just before the Javascript. “window.open”, the “response.write” would execute immediately just as expected.
Next I replaced the “window.open” with an “alert("Hello World!")” and found that it was just as slow as the “window.open”.

This shows me that for some reason the Javascript is running slow.
We have other programs that utilize Javescript without any problems so I doubt that it is directly related to the server.

Any ideas why?
 
It sounds like the page is not buffering it's output, but instead sending it straight to the client. Your placement of the Response.Write seems to back that up for me.
If the javascript is executing based on the onLoad from the body tag then it has to wait until the whole page has been loaded before executing, whereas the ASP is executed and then sent directly to the client as part of the page generation.
Another possibility is that the page the window.open command is opening could be the one responsible for the slowdown. Try loading that page directly in a browser and see if it loads slowly.

Placement of the window.open could be important. If it is triggered in some fashion by the onLoad event, then as I said above it has to wait for the entire page to load before doing anything. A more valid test would be to place a Response.Write at the very bottom of the page and have it write somthing like "Page has finished loading". Once that final bit of text shows up the window.open should execute almost immediately because the browser has finished loading the page/file. It still has to register all the events, though. (onMouseClick, onLoad, etc). If you have a lot of events declared in the page than this will take a while. At one point I had a page that was loading and declaring on the order of 15,000 mouse over events, it would nearly lock up my computer as the browser tried to register listeners/handlers for each individual event. By switching to a single document.onclick event and checking the source of the event fire I was able to speed up the page to load in under 2 seconds instead of over 30 seconds with 100% cpu usage.

ASP things to look for that will make the page seem to load faster than it really is:
Is the Response.Buffer = false?
Are there Response.Flush's throughout the code?
Is there a large amount of code after the presentation doing internal work? (ie, writing values back to a db or cleaning up files, or whatnot)
Are all of the objects properly set to nothing before exiting the script?

Javascript things to look for:
Is the window.open based off the onLoad event in the body tag?
Are there a large number of events in the page? (onClick, onMouseover, etc)
Are there any SetTimeout's being registered/run?
Is there a great deal of javascript being run during the onLoad procedure that might make it take longer to get to that window.open?

Hopefully some of these will help ascertain the problem, let us know if any or none of these result in you finding the problem, we may be able to help you clean it up some.

-Tarwn

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
minilogo.gif alt=tiernok.com
The never-completed website
 
Tarwn,

Thanks for the response. You helped me figure it out.

The “window.open” was near the top of the page and was based on an “if” statement. If true, execute the “window.open” and open a different page.

Otherwise it would run all of the code remaining on the page, which was a LOT and included html, sql queries, and JavaScript. This is what appears to be slowing down the process. Obviously all/some of the remaining code has to be completed before the JavaScript can execute.

What I have done is make the “if” statement into an “If Else” statement. That saves it from having to load everything on the remaining part of the page. Now I’ll work on speeding up the remaining part of the page when the “else” is used.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top