34 users online (0 members and 34 guests)  


  Results 1 to 6 of 6
  1. #1
    paul9038's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2004
    Posts
    8

    Question Help with more efficient script.

    If anyone has or can help with making these 2 functions more efficient i would appreciate it. The first function takes the selected items from one select list and adds them to another, however the select lists in question can get large, i.e. 300 items each. The problem here is that the system can hang / take 10 minutes to process the operation.
    *****************First***********************
    function AddAll(Source, Destination){
    var i, j, itemexists, nextitem;
    //Step through each unit count item selected.
    for (i = 0; i < Source.options.length; i++){
    //Search to see if this item has already been added to the query list.
    j = 0;
    itemexists = false;
    while((j < Destination.options.length)&&(!(itemexists))){
    if(Destination.options[j].value == Source.options[i].value){
    itemexists = true;
    }
    j++;
    }
    if(!(itemexists)){
    //Adds an item if it does not already exist in the select query.
    nextitem = Destination.options.length;
    Destination.options[nextitem] = new Option(Source.options[i].text);
    Destination[nextitem].value = Source.options[i].value;
    }
    }
    document.main.fieldCount.value = Destination.options.length;
    }
    *******************************************
    The second function removes items selected from the select lists mentioned above and it works fine except if the user wanst to deselect a very large number of items from a list i.e. the user selects 300 items from a 600 item list, which takes minutes to process and delete and srews up the display for the duration of the processing.
    ****************Second*********************
    function Remselection(Source){
    var i = 0, k = 0;
    //Step through each Query item to delete the selected ones.
    while(i < (Source.options.length - k)){
    if(Source.options[i].selected){
    //Remove this item.
    for(j = i; j < (Source.options.length - 1); j++){
    Source.options[j].text = Source.options[j+1].text;
    Source.options[j].value = Source.options[j+1].value;
    Source.options[j].selected = Source.options[j+1].selected;
    }
    k++;
    } else {
    i++;
    }
    }
    for(i = 0; i < k; i++){
    Source.options[Source.options.length - 1] = null;
    }
    document.main.fieldCount.value = Source.options.length;
    }
    *******************************************

  2. #2
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Hi there,

    To be honest, JavaScript on non-hugely spec'd PC's is going to have trouble with very large lists. Its just the nature of the beast. I notice some looping code in there. Thats probably where its taking so long.

    Have you considered moving away from this approach completely? I am pretty sure any use would rather load a second page than wait ten minutes for a script to finish.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  3. #3
    paul9038's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2004
    Posts
    8

    Thumbs up Script efficiency

    Just to update you on this issue. I researched some opinions on processing lists in javascript and as you said, unless the system is a high spec its going to be tough work. The previous code provided was taking 5 to 10 minutes approx. to process a 250 item list into a 300 item list on a P4 and crashing the browser on anything less. However it seems that the more references being made to form objects the slower your code will be as the objects are stored in various hierarchies of arrays i.e. windows array, forms array etc. Basically the more dot operations that you can remove the faster your code. So I tried this by building arrays and variables to replace as many dot operations as I could, and it worked brilliantly. So instead of a loop that checked the .value property of one form list against the .value property of another in a nested loop I built 2 arrays from the form lists outside the nested loop and just checked the values at these array indexes within the nested loop. A huge performance increase was achieved.

  4. #4
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Excellent. Thanks for letting us know.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  5. #5
    HTML's Avatar
    Administrator

    Status
    Offline
    Join Date
    Aug 2000
    Posts
    3,445

    Follow HTML On Twitter Add HTML on Facebook Add HTML on Google+ Add HTML on Linkedin Visit HTML's Youtube Channel
    I feel like a first grader reading a german version of war and peace.

    D

  6. #6
    Enigma's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Nov 2000
    Posts
    426
    You and me both, lol.



Tags for this Thread