mardi 28 avril 2020

Loop with If-statement runs slow, too slow

I am new to JavaScript. I use it to write some scripts for Adobe Illustrator. In this script I take a selection of items, and sub-select them by some user definded values (xPointMin, xPointMax, etc.). The loop below is the main part of the function.

My problem is that this loop is terribly slow. It takes several sec. to run a selection of 50 items.

I already tried the following:

  1. run the loop without any if-condition inside. This is fast.
  2. run the loop with only one if-condition inside. This is as fast as with all if-conditions.

Does someone know why it is so slow or can some just make it faster.

// xArray... are extracted vales from the Selection (xSel)
// xPointMin, xPointmax, xLengthMin, xLengthMay, xAreaMin, and xAreaMax are user defined values

for (var i in xSel) { // xSel is a list of selected items
  var xTF = true; // xTF is temporary variable

  // points // this will check if the given value (xArrayPoint) is within the requirements
  if (xArrayPoint[i] <= xPointMin || xArrayPoint[i] >= xPointMax) {
    xTF = false; // if so it sets the temporary variable to false
  }

  //length // same as in the first check, however we are testing the length
  if (xArrayLength[i] <= xLengthMin || xArrayLength[i] >= xLengthMax) {
    xTF = false
  }
  //area // same as in the first check, however this time we are testing area
  if (xArrayArea[i] <= xAreaMin || xArrayArea[i] >= xAreaMax) {
    xTF = false
  }

  xSel[i].selected = xTF; // changes the original value
}
}

Aucun commentaire:

Enregistrer un commentaire