[Project_owners] Optimizing a looping function

Tracy Norris tnorris at nc.rr.com
Sun Sep 12 17:18:57 EDT 2004

Why not sort the list by email address first, this should allow you to 
do all your comparisons with one pass...You might also sort first by 
email address, then subsort by length of display name (descending), 
length of first name (descending), length of last name (descending) and 
then just take the first of a set of email addresses, or something like 

Mike Krieger wrote:

> Hi all,
> I've been working on the next version of my addressContext extension 
> and have run into a problem on the issue of checking for duplicates 
> before adding all the addresses to a Mailing List. The current logic 
> for what I'm doing is the following:
> 1. Take the first address and the second address
> 2. If the email addresses are the same:
>   a) and they both have display names attached ("John Doe" 
> <john at doe.com> and "John" <john at doe.com>, then take the one with the 
> longest display name
>   b) and the first name is blank but the second name is there, take 
> the second name
>   c) and the second name is blank but the first name is there, take 
> the first name
>   d) and both names are blank, just take the first one
> 3. Take the third address, and compare it to the first, and so forth.
> Unfortunately this is very CPU-intensive, and Thunderbird throws up 
> all sorts of "This script is taking too much CPU time, would you like 
> to Cancel?" errors (and then gives you "OK" and "Cancel" buttons, 
> which is totally counter-intuitive - Cancel the Cancel? - but I 
> digress). What I would like to know if anyone can think of a more 
> efficient way of doing this. I could follow the suggestion from a few 
> months ago on the list using a setTimeout( but before that I'd like to 
> optimize this function as much as possible. Here is the code - the 0th 
> term of the array is the email address and the 1st term is the display 
> name:
> if (firstToCompare[0] == secondToCompare[0]){
>  if (firstToCompare[1] !== null && secondToCompare[1] !== null){
>    if (firstToCompare[1].length > secondToCompare[1].length){
>      addressArray[j] = null}
>    else {
>      addressArray[i] = addressArray[j];
>      addressArray[j] = null};
>  }
>  else if (firstToCompare[1] !== null && secondToCompare[1] == null){
>   addressArray[j] = null}
>  else if (firstToCompare[1] == null && secondToCompare[1] !== null){
>   addressArray[i] = addressArray[j];
>   addressArray[j] = null}
>  else if (firstToCompare[1] == null && secondToCompare[1] == null){
>   addressArray[j] = null}
> }
> Thanks tons in advance!
> Mike K
> _______________________________________________
> Project_owners mailing list
> Project_owners at mozdev.org
> http://mozdev.org/mailman/listinfo/project_owners

More information about the Project_owners mailing list