This for has a algorithmic efficiency of 2^n. In other words it completes two tasks each iteration (n standing for the total number of repeated iterations)Those two tasks are checking the current value of the next index, and then comparing it to the current max value. It then needs to repeat this process for the entire list of values. As you mentioned, this would not scale well at all. This becomes further complicated as positional data is usually stored as a 2 value (X and Y) or 3 value array (3D Z Position, X, and Y). This requires the additional use of the variable a in your code. But there is an easy way to reference to a specific dimension value, and that is by it’s index.
transform.Position.value[0] = X Value
transform.Position.value[1] = Y Value
transform.Position.value[2] = Z Value (If you are working with 3D Layers)
_______________________________________
try this….might work and might not….I am typing it out here and not in an IDE or AE Expression Field.
———————————————>
max = thisComp.layer(“Shape Layer 1”).transform.Position.value[0];
for (i = 1; i<=thisComp.layer(“Shape Layer 1”).transform.Position.numkeys; i++) {
max = Math.max(max, max.key(i).value);
}
max
The example above might not be perfect but it removes at least one step of assigning an A variable each iteration. As well as eliminates the need to have the algorithm manually assign a temp value to Max by extracting the X Position Data. This instead just assigns the value of the Shape Layers X Position Value by referring to it by it’s array index.
As mentioned this code may not work as I am typing it out here. I would also suggest assigning a different variable name for max such as maxXPosVal so it doesn’t get confused with the Math.max method you are calling. It will also make future code review easier when you have spent some time apart.
I hope this all made sense, and I also hope I didn’t just make a fool of myself with the code. I just started JavaScript recently but have tons of experience with Java. Pretty similar, just slightly different syntax.
Let me know if this solves your query, or at least help you along in the right direction.