Long time to access API data in Photoshop v.24

(Hi, my first time posting, any recommendations to improve this post are welcome. Thank you.)

I came across what could be a bug. (tried Photoshop v.24.2 and v.24.3)
This block of code takes ≈5.5 seconds.

var layer = app.activeDocument.activeLayers;
var theOneLayer = layer[0];
// tried accessing data directly, with no difference
// var otherLayers = [theOneLayer.layers[0].name, theOneLayer.layers[1].name, theOneLayer.layers[2].name, theOneLayer.layers[3].name, theOneLayer.layers[4].name];
var otherLayers = theOneLayer.layers.map(l => l.name);
// var otherOtherLayers = [theOneLayer.layers[0].layers[0].name, theOneLayer.layers[2].layers[0].name, theOneLayer.layers[4].layers[0].name];
theOneLayer.layers.forEach(layer => {
  if (layer.layers) {
    otherLayers.push(layer.layers.map(l => l.name)[0]);
  }
});

Tried it also with Photoshop v.23.5.5 and this code finishes instantly.

Hello, did you try multiple runs and did you use the same document? Sometimes the first run can take a bit longer than the subsequent one. I just want to make sure that it is measured correctly before digging deeper :slight_smile: Also could upload the PSD file please so I could try it myself? If I could reproduce issue I could possibly fix it.

1 Like

Hello.
Yes, definitely tried it multiple times and with different approaches.
Funnily, first load of the plugin was a bit faster than the later ones :slight_smile:
I tried uploading .psd file, but it’s not a permitted format to upload.
Here’s the hierarchy of my PSD file:

That is it? 3 layers and 4 groups? That really shouldn’t take 5.5 seconds.

The problem with DOM is that it does not cache anything so even if you don’t change anything in document it always ask PS for fresh data again and again which can add up when you have a lot of iterations.

Exactly. I don’t understand why would this take so long to process. It’s instantaneous on older versions.

Does it not cache when I save it inside the variable ‘layer’?

Well it stores the instance of layer. But when you ask for layer name it asks Photoshop and gets result back. So name itself is not stored in layer. There is stored only a getter who knows how to get layer name. Otherwise you would get really easily out of sync and would be easy to make error.

1 Like

Hi, could you try to run your code inside executeAsModal?

I had low perfomance issue on 24.2 getting layer bounds with large count of nested layers, and executeAsModal helped to reduce parse time.

2 Likes

Hi, tried it and yes. With executeAsModal the execution time dropped to a few miliseconds.

1 Like

The problem with Modal Execution is that other plugins can’t run during that period. This is the problem in cases when your plugin reacts to some events. If that reacts to button click then it should be fine :slight_smile: