Your code isn't too bad actually, however it can indeed be optimized. It's better to store your trusted emails in an array rather than a comma-separated string. This also allows you to loop over the array instead of the chat contacts.
Javascript code:
var TrustedEmails = ["john@hotmail.com", "smith@hotmail.com"];
function ConfirmTrusted(ChatWnd){
var i = 0, item;
while(item = TrustedEmails[i++]) {
if(ChatWnd.Contacts.GetContact(item) !== null) return true;
}
return false;
}
First, the used variables are declared at the top:
i is the index iterator and
item is the item iterator.
The while-condition combines the iterating (incrementing
i and assigning
item to the
i-th array element) and checking the array length (if
i exceeds the array's length,
item will be undefined and thus the condition is false), making it one of the fastest ways to loop through arrays in JScript. Note that this only works for arrays where each element equals true, your array can't contain zero, false, null, undefined or empty strings.
Inside the loop, we try to find the current item in the Contacts object using GetContact(). This function returns a Contact object when the passed email is in the list, and null otherwise. Thus, to check whether the trusted email is in the chat, we simply have to see if the return value is not null. If it's not null, it means the trusted email is in the chat and thus we should return true.
As you can see, this optimizes the function by reducing the work on the JScript side. Your original code loops through all contacts in the chat and then performs an indexOf operation to see if the current chat contact is in the trusted emails list. This code loops through all trusted emails and calls a Plus! function to see if the current trusted email is in the chat contacts list. By changing the looped list, we can let Plus! do some of the work for us, which is probably much faster than any script code you write.