ExtendScript in Photoshop: traversing layer list faster
Hi guys,
I'm working on a complicated script that makes use of layer/group hierarchy and directives in layer names. To get the process started, the first thing the script does is recursively traverse the whole layer tree to collect the data and create js objects with all the necessary information I need to proceed.
Right now I have a recursive function that takes a layer set, and then basically does this:
function AnalyzeGroup(group) {
for (var i = 0; i < group.layers.length; i++) {
var layer = group.layers[i];
// ...work with layer...
if (layer.typename == 'LayerSet')
AnalyzeGroup(layer);
}
}
My problem is, this process is ridiculously, unbelievably slow. The delay can already be felt at dozens of layers, but some of the documents I need this to run on contain hundreds or even thousands of layers, and the delay grows fast. Traversing an average document might take 5 seconds, but today I was brave enough to run the script on a document with 2000+ layers, and the initial stage took literally 15 minutes.
I understand that this is probably not a "normal" usecase, and I suspect nothing can be done to improve this, but taking 15 minutes to trivially traverse a thousand items doesn't feel right to me, so maybe I'm missing something essential here? Idk.
As I understand it, simply accessing document elements via this API is very costly for some reason. If I could somehow, say, instantly receive a quickly accessible copy of the whole hierarchy to process, it would be fantastic. Although I do need full info on hierarchy, layer names and visibility... and, well, some sort of reference to the layer objects themselves, as I use them later during the actual export.
I don't have insanely high hopes for solving this, but any advice will be greatly appreciated. Thanks for hearing me out!
