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!

speeding up loop 1

Status
Not open for further replies.

birney29

Programmer
Oct 11, 2001
140
GB
Hi there,

i use javascript to populate a drop down from an object, problem is, my lists can run to 100's or 1000's of entries. This causes them to populate very slowly. i know from a user interface point of view, this is bad design,but my hands are tied to using drop downs. ill post an example below, if you can think of anyway to speed it up, id be very grateful.

Code:
object (in top frame)

var GunTypedropDownObject = [{ value:  "", text: "" },
{ value:"238.003", text:""},
{ value:"237.003", text:"Monicas Test"},
{ value:"235.003", text:"//TEST34"},
{ value:"236.003", text:"//TEST34"},
{ value:"2.003", text:"22 Centrefire Rifle"},
{ value:"81.003", text:"Action (RR)"},
{ value:"82.003", text:"Action Only"},
{ value:"83.003", text:"Adapter"},
{ value:"85.003", text:"Air Pistol(CO2)"},
]

function

function fillGunTypeSelect() {
	
        var select = document.getElementById( 'GunType' );
        var options = select.options;
	
	If(options.length == 1)
	{
        options.length = 0;
	
        
	  var gtdo=top.headerFrame.GunTypedropDownObject;
		for(var gi=0;gi<gtdo.length-1;gi++)
		  {
		  onegtdo=gtdo[gi];
		  options[options.length] = new Option(onegtdo.text, onegtdo.value);
		  }
	}
	else
	{
	}
	 
    }

thanks for the help
Kenneth Birney
User Interface Programmer
Scottish Police
 
I recommend using Option() instead of full objects (I know that sounds redundant because arrays are object in JS)

let me explain why :

var GunTypedropDownObject = [{ value: &quot;&quot;, text: &quot;&quot; },
{ value:&quot;238.003&quot;, text:&quot;&quot;},
{ value:&quot;237.003&quot;, text:&quot;Monicas Test&quot;},
{ value:&quot;235.003&quot;, text:&quot;//TEST34&quot;},
{ value:&quot;236.003&quot;, text:&quot;//TEST34&quot;},
{ value:&quot;2.003&quot;, text:&quot;22 Centrefire Rifle&quot;},
{ value:&quot;81.003&quot;, text:&quot;Action (RR)&quot;},
{ value:&quot;82.003&quot;, text:&quot;Action Only&quot;},
{ value:&quot;83.003&quot;, text:&quot;Adapter&quot;},
{ value:&quot;85.003&quot;, text:&quot;Air Pistol(CO2)&quot;},
]
is slow since later you use the same objects to create Options.


Try using an array of options instead :

var GunTypedropDownArray = [new Option(&quot;&quot;,&quot;&quot;),new Option(&quot;238.003&quot;,&quot;&quot;),etc...]

Now to loop through it all use array reference instead :

for loop here
{
myObject = GunTypedropDownArray[index];
}

Now to fully speed up your loop try reading the FAQ I wrote in this forum on how to speed up javascript up to ten times! :)

I hope this helps. Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top