7 Replies Latest reply on Oct 16, 2012 2:30 PM by CarlosCanto

    selected property == inherently slow ?

    W_J_T Level 4

      Hello everyone,

       

      So I am working on a script and trying to select items from an array by applying the "selected" property to those items, which seems to be extremely slow? The rest of the code executes almost instantly, and when I replace the property "selected" to something else it proves that "selected" is indeed the culprit. I don't know the underlying factors but the "selected" property seems quite slow to utilize in such a case.

       

      Is there some way I can instantly apply selection to items in an array without having to iterate through a loop (however this does not seem to be the issue as mentioned above), or even use the "selected" property to achieve this? Maybe something within the scripting reference or javascript which can assist me, maybe that I have failed to find or think of.

       

      If someone can enlighten me on some possible faster approaches I would be very grateful. Many thanks in advance!

        • 1. Re: selected property == inherently slow ?
          CarlosCanto Adobe Community Professional & MVP

          Hard to tell without looking at your script, is selecting objects your ultimate goal? or is it just a step to do something else?

          • 2. Re: selected property == inherently slow ?
            W_J_T Level 4

            Hi CarlosCanto,

             

            Thanks for responding. Yeah its solely for selection based on other factors, so only the items contained in the array are needed. Everything works as stated virtually instantly, but using "selected" is very slow, thus I was hoping for another possible approach for better speed.

            • 3. Re: selected property == inherently slow ?
              CarlosCanto Adobe Community Professional & MVP

              can you post your code? we might be able to spot what's wrong or to suggest alternative solutions if there's nothing wrong with it.

              • 4. Re: selected property == inherently slow ?
                W_J_T Level 4

                Hi CarlosCanto,

                 

                Thanks for getting back with me, sorry for the delay in responding it was getting late last evening. Anyway, I have done further tests this morning and as stated "selected" is inherently slow? The initial code for populating the array happens instantaneously so I know thats not the issue here. The following are some loop tests I have tried on the array once populated, none of which really make a tremendous difference when setting the "selected" property.

                 

                //

                // Various Loop Variations Tested:

                //

                // 1.

                var i = selectionArray.length;

                while (i--) {

                    selectionArray[i].selected = true;

                }

                // 2.

                for (var i = 0; selectionArray[i]; i++) {

                    selectionArray[i].selected = true;

                }

                // 3.

                for (var i = 0, len = selectionArray.length; i !== len; i++) {

                    selectionArray[i].selected = true;

                }

                // 4.

                for (var i = selectionArray.length - 1; i >= 0; --i) {

                    selectionArray[i].selected = true;

                }

                 

                All results are fairly similar using "selected", however when changing to remove() or even width or another property again there is a huge discrepancy in execution time, thus my question about why is "selected" so darn slow and can anything be done to overcome it. When testing this, this is true for array length tests of 1000 - 15000, "selected" is very slow in comparison to other properties being quite fast. For example with an array of approx 13000, remove() takes 2 seconds, using width takes 187 seconds (3 min), while "selected" takes approximately 1476 seconds (24 min), this is maybe an extreme test but shows the point. Obviously these three property tests demonstrate there are internal differences as to whats going on, but why is selection so darn slow comparatively? I assume it is due to it being a boolean and needing to be evaluated but man its so slow.

                 

                Is there anything that can be done, or another approach for selecting the items faster which are contained with in the array? I mean for example selectObjectsOnActiveArtboard is virtually instantaneous (Test: 25000+ in 1 second) is there something similar or another approach for my case of items with in an array?

                 

                -----

                 

                [Q:] A Related Question: what Javascript engine is embedded in the suites or ESTK and what version of JS is it using/targeting? Just wondering if there are any specific optimizations that can be considered or kept in mind in general based on the specific JS engine when coding.

                 

                -----

                 

                Thanks CarlosCanto, and others for reviewing this and offering any feedback, many thanks in advance.

                • 5. Re: selected property == inherently slow ?
                  CarlosCanto Adobe Community Professional & MVP

                  selection is always a problem, I think it has to do with redrawing the screen each time you call it, and even unsing app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; doesn't seem to help.

                   

                  an alternative way would be to loop thru your object array and add a note the each of them, the manually run an action to select items with such note. It'll select items instantly, well, whatever it takes you to play the action.

                   

                  if you can use applescript or vbscript you could run the action automatically within your script, Javascript doesn't support this command at least up to CS5...did I hear CS6 does? if you have CS6 you can check on that.

                  1 person found this helpful
                  • 6. Re: selected property == inherently slow ?
                    W_J_T Level 4

                    Hi CarlosCanto,

                     

                    Thanks so much for your response and information. I don't have CS6 yet, just CS5.+, but it's good to know that there are some *cough* advancements there concerning scripting.

                     

                    Ah, problems and limitations, that seems to be the ongoing theme I find when searching about scripting illustrator. It does offer an opportunity for critical thinking, creative approaches and after the dust settles from head banging, sometimes working solutions. I think your right about the redraw and perhaps also coupled with the boolean of the property itself, it becomes very slow.

                     

                    Anyway your last post pushed my thinking further thankfully. I ended up moving the items to a designated selection layer and using hasSelectedArtwork (of course once made its easy to then toggle the selection also in the layer palette manually, if desired). "Move" seems instantaneous and I can always merge things back if needed. [Q:] Does "move" work like paste in place? It seems to, but just asking to make sure things are keeping position and just moving layers.

                     

                    Thanks again CarlosCanto, for the insight and support, really appreciate it.

                    • 7. Re: selected property == inherently slow ?
                      CarlosCanto Adobe Community Professional & MVP

                      *cough* advancements there concerning scripting.

                      hahaha you don't see those two words in the same sentence very often...in a positive way I mean

                       

                       

                      you can think of "Moving" as in from Container to Container, as opposed to x,y movement.

                      1 person found this helpful