【发布时间】:2020-04-28 21:18:26
【问题描述】:
我是 JavaScript 新手。我用它为 Adobe Illustrator 编写一些脚本。 在这个脚本中,我选择了一些项目,并通过一些用户定义的值(xPointMin、xPointMax 等)对它们进行子选择。下面的循环是函数的主要部分。
我的问题是这个循环非常慢。这需要几秒钟。运行 50 个项目的选择。
我已经尝试了以下方法:
- 在没有任何 if 条件的情况下运行循环。这很快。
- 运行循环,其中只有一个 if 条件。这与所有 if 条件一样快。
有人知道它为什么这么慢,或者有人可以让它更快吗?
// 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
}
}
【问题讨论】:
-
从来没有为 illustrator 做过任何事情,如果你做
console.log你能看到输出吗?如果是,也许你想玩console.time("timer_name);然后记录时间console.timeLog("timer_name")并结束使用console.timeEnd("tiner_name"),您可以看到(以毫秒为单位)每个日志的时间量... -
我使用 Date.now() 来跟踪时间。这样,我将其缩小到上面显示的代码和给定的持续时间(“慢”~4000 毫秒;“快”~50 毫秒)
标签: javascript for-loop if-statement adobe-illustrator