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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very slow javascript code.. 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anyone know how a better way to implement the following JavaScript code, because loading the page it’s terrible slow with larger text elements in the array?

function load_text()
{
text = new Array();
text[0] = "Something";
text[1] = "Something x";
text[2] = "Something xx";
text[3] = "Something xxxx";
}

In another function:

document.form.textarea.value = text[number];


Thanks in advanced,

Bo
 
One thing that will help, if all the elements are in the same form, in the funciton do something like this:

function setData()
{
var frm = document.form
frm.textarea.value = text[number];
} jaredn@eae.net -
 
Should've explained my answer, sorry:

This will only help performance if you are setting several of the fields values with one call to the function. I assume you are using a loop of some sort? The reason this helps, is ebcuase you are caching, in memory, the location of document.formname. This way, the interpreter no longer hs to search through the object hierarchy each time you access one of the elements. jaredn@eae.net -
 
Thanks jaredn for your help, but unfortunately is the performance the same!

The function is supposed to get the text on a mouseover (onmouseover=”get_text(x)”) – but when the array elements are large (text elements on 250-300 characters) is the loading of the page very slow (10-15 sec.).

With small text elements is there no problem!

<script language=&quot;JavaScript&quot;>
<!--

function load_text()
{
text = new Array();
text[0] = &quot;Something&quot;;
text[1] = &quot;Something x&quot;;
text[2] = &quot;Something xx&quot;;
text[3] = &quot;Something xxxx&quot;;
}

function get_text(number)
{
document.text_form.picture_text.value = text[number]
}

-->
</script>

Do you have any other suggestions?

Regards,

Bo
 
Try passing them as strings instead (may help):

function load_text()
{
text0 = &quot;Something&quot;;
text1 = &quot;Something x&quot;;
text2 = &quot;Something xx&quot;;
text3 = &quot;Something xxxx&quot;;
}

function get_text(str)
{
document.text_form.picture_text.value = str
}

get_text(text0) jaredn@eae.net -
 
No unfortunately it didn’t, but I found out something weird!

The performance of the script is though closely related to the amount of characters in the text, where I tested have observed the following:

Test 1)

10 text elements with exactly 15 charaters each is okay (loading time < 1. sec.)
10 text elements with exactly 16 charaters each is NOT okay (loading time > 10 sec.)

Test 2)

4 text elements with exactly 138 charaters each is okay (loading time < 1. sec.)
4 text elements with exactly 139 charaters each is NOT okay (loading time > 10. sec.)

I cant se any relations between the two tests, except a significant time delay when I increase the amount of charters with one!!

I have tried with an array, but whit out any performance changes!

Does anyone know how to overcome this problem – so the loading of the page becomes quicker?

Thanks in advanced,

Bo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top