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
我是一个新的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
}
}
只运行一个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
)
);
有谁知道为什么这么慢,或者有谁能让它更快。
我是JavaScript的新手。我用它来为Adobe Illustrator写一些脚本。在这个脚本中,我选择了一个项目,并通过一些用户定义的值(xPointMin,xPointMax等)对它们进行子选择。下面的代码可以为你节省很多时间。让我们看看我们在这里做了什么。我们做了两个重大的改变
将多个if条件简化为一个赋值语句。
: 由于所有的条件都在OR操作数下,如果第一个条件为真,tt就不必通过每个条件。
劲文
如果循环很小,你就不会明白为什么要花这么长的时间,但是条件就没有什么意义了,比如说。如果 (xArrayPoint [i] <= xPointMin)