| xArrayPoint [i]> = xPointMax)

问题描述 投票:0回答:1
This is clearly true, either one value is greater than another or it is less than or equal to another, there are only these 3 possibilities, its ifs are equivalent to if (true).

Something that may make a difference (but I'm not sure about that in javascript) is to create variables inside a loop, maybe that will be expensive in terms of processing in having to allocate space in memory to save this data.

Something that optimizes a lot in search of values ​​in variables for example is the

binary search algorithm

  1. , which is something very simple.Say for example that you want to search for a certain value within an array, the idea of ​​the binary search is based on first placing these values ​​in ascending order and comparing the value you are comparing with the intermediate value of that array, if its value is for example higher than the intermediate value (smaller is the same idea) it will compare with the average value that is between the highest value of the array and the average value of the array, that is, it will divide the array in two until it finds the value sought.

我是一个新的JavaScript,我用它为Adobe Illustrator写了一些脚本。在这个脚本中,我使用它为Adobe Illustrator编写了一些脚本。在这个脚本中,我选择了一个项目,并通过一些用户定义的值(xPointMin,xPointMax等)对它们进行子选择。下面的循环是函数的主要部分。

// 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
}
}
我的问题是这个循环非常慢。运行一个50个项目需要几秒钟的时间。
javascript for-loop if-statement adobe-illustrator
1个回答
1
投票

只运行一个if条件的循环。这和所有if条件一样快。

array1.forEach(i => 
    xSel[i].selected = !(
        xArrayPoint[i] <= xPointMin || xArrayPoint[i] >= xPointMax || 
        xArrayLength[i] <= xLengthMin || xArrayLength[i] >= xLengthMax || 
        xArrayArea[i] <= xAreaMin || xArrayArea[i] >= xAreaMax
    )
);

有谁知道为什么这么慢,或者有谁能让它更快。

  1. 我是JavaScript的新手。我用它来为Adobe Illustrator写一些脚本。在这个脚本中,我选择了一个项目,并通过一些用户定义的值(xPointMin,xPointMax等)对它们进行子选择。下面的代码可以为你节省很多时间。让我们看看我们在这里做了什么。我们做了两个重大的改变

选择 forEach 而不是传统的 for

0
投票

将多个if条件简化为一个赋值语句。

: 由于所有的条件都在OR操作数下,如果第一个条件为真,tt就不必通过每个条件。

劲文

如果循环很小,你就不会明白为什么要花这么长的时间,但是条件就没有什么意义了,比如说。如果 (xArrayPoint [i] <= xPointMin)

© www.soinside.com 2019 - 2024. All rights reserved.