Why Is This Piece Of Javascript Code So Slow?
Solution 1:
Okay, you're not going to believe this. I just tried removing the following line:
window.location.href = url.toString();
And it reduced the average runtime to two third of a millisecond. I know from profiling the toString call is very fast, so apparently setting the window.location.href is ultra slow (couple of hundred ms!!!). Bah, I hate IE.
Note: This is a clean install of Internet Explorer, and I have no crazy toolbars slowing down my browser.
Solution 2:
Seems that you store fields from some form.
Instead of using document.getElementById()
to get each element of form try to get value of form elements directly:
navState.currentTab = document.formName.currentTab.value;
where formName
is value of name
attribute of form
tag and currentTab
is value of name
attribute of form element (i.e. input, checkbox).
EDIT:
When I was using IE5 and IE5.5 in 2000 even change (store reference to form element in variable) from:
for (i = 0; i < document.form.elements.length; i++) {
values[i] = document.form.elements[i].value;
}
to:
var form = document.form;
for (i = 0; i < form.elements.length; i++) {
values[i] = form.elements[i].value;
}
made big difference.
I am afraid nothing has changed during last 10 years :(.
Solution 3:
Have you tried commenting out the "get all vars" section and the window.location.href
line? It may be one of the input
s or navigation (e.g. buggy browser toolbar) that's causing the delay.
By the way, it works fine on my test page, but it may be that you have a much larger DOM.
Post a Comment for "Why Is This Piece Of Javascript Code So Slow?"