1

Am using nested if to iterate through a list of window attributes and am deleting the window attribute based on a criteria. Since this s a nested loop, the execution time takes .5seconds. We have to make it quicker and bring it down to millisecs. the problem is we have a lot of automation scripts to run our regression tests and since the document object takes to reload during iteration the automation test pack fails to find elements in the html and throws error in most of the cases.

We asked them automation team to leave a thread.sleep or wait between every action they do but since they have more than 200 test scenarios it's difficult for them to add the time delays in every single action so it has come back to the dev to check on improving the performance.

Please suggest best optimal solution.

I tried multiple iterations to check on the execution time but traditional for seems to beat the rest of it.

Snippet:

function resetWindow(){
  const ALL_WIN_KEYS = Object.keys(window);
  for(let i=0;i<ALL_WIN_KEYS.length;i++){
     let matchFound = false;
     for(let j=0;j<DEFAULT_WIN_KEYS.length;j++){
      if(ALL_WIN_KEYS[i] == DEFAULT_WIN_KEYS[j]){
         matchFound = true;
         break;
      }
     }
     if(!matchFound){
       delete window[ALL_WIN_KEYS[i]];
     }
  }
}

Note: DEFAULT_WIN_KEYS is a const declared globally.
Steffie
  • 11
  • 2
  • Added the image. Please check. – Steffie Jan 20 '23 at 07:51
  • 1
    ok Thanks. Removed image and added code snippet. – Steffie Jan 20 '23 at 08:58
  • 1
    If `DEFAULT_WIN_KEYS` never changes, why is it not declared as an object (or a class)? If you do it like that, then you just need to do a lookup, instead of loop through it every single time. – Rickard Elimää Jan 20 '23 at 09:03
  • What Rickhard has said, additionally cache `ALL_WIN_KEYS.length` into a variable and compare to that variable in `for` condtion part. Another story is how useful deleting the keys from `window` is. `delete` operator just deletes the keys, it doesn't delete the values from the memory, only Garbage Collector can do that, and it never visits in the global scope. – Teemu Jan 20 '23 at 09:16

0 Answers0