15 Replies Latest reply on Nov 2, 2018 3:10 PM by De Pascalis

    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
                      • 8. Re: selected property == inherently slow ?
                        De Pascalis Level 1

                        I'm having the same issues so I'm bringing this old thread back to life.

                         

                        I was wondering if someone has some new solutions for this?

                         

                        I've just started on my first script for Illustrator and I immediately ran in to this issue. Below is my work in progress.

                        What I want to do is to filter my selection of items. When setting activeDocument.selection equal to my filtered array, it looks like it's updating the activeDocument.selection array object by object from the filtered array. I want to first make all the changes to activeDocument.selection and then tell it that "the selection array is now done, re-render the screen". In other words, Illustrator should not update anything before my function has completed.

                         

                        function filterSelected() {
                        
                          var aDoc = activeDocument;
                          var sn = aDoc.selection;
                          var filteredSn =  []
                        
                          // Test each item in selection array
                          for (var i = 0; i < sn.length; i++) {
                            // If item is a Path or a Compound Path
                            if ( sn[i].typename == "PathItem" || sn[i].typename == "CompoundPathItem" ) {
                              // Item bounds area excluding stroke width
                              var boundsArea = sn[i].width * sn[i].height;
                              // And if item bounds area is smaller than #
                              if ( boundsArea <= 0.05 ) {
                                // Put item in filteredSn array
                                filteredSn.push(sn[i])
                              }
                            }
                          }
                          // SLOW PART!
                          aDoc.selection = filteredSn;
                        }
                        
                        filterSelected();
                        
                        • 9. Re: selected property == inherently slow ?
                          Silly-V Adobe Community Professional

                          Looping through items has always been the thorn in our existence. I was not aware of the slowness associated with just assigning a selection object, but I have a suggestion which may work: assign a note to your filtered items, then at the end instead of using code to set the selection property of the document - run an action which selects 'all whose note has...' and your filtered items should be selected instantly. Then perhaps it's still a matter of cleaning up your notes to remove them after being used for this purpose.

                           

                          But - actually, since you are looping anyways through a selection, why not go through it backwards and simply de-select the items you do not want?

                          • 10. Re: selected property == inherently slow ?
                            De Pascalis Level 1

                            Thanks Silly-V

                            The note solution was mentioned higher up in this thread. I have not tested that yet since I thought it was to much of a hack, that's why I asked if any new solutions has surfaced since then. Looks like I have to try that though.

                             

                            I was thinking about your other solution myself but thought - without trying it - that it would not make any difference, it still would re-render after each de-selection.

                             

                            I get why JS loops can be slow, but I don't think that's the issue in this case. As I mentioned, I believe the problem is that activeDocument.selection is being updated at the same time, and for each time it takes a new object from the other array.

                             

                            Like this:

                            var myArray = ["one", "two", "three"];

                             

                            activeDocument.selection = myArray;

                             

                            // Behind the scenes, selection updates

                            activeDocument.selection ["one"];

                            activeDocument.selection ["one", "two"];

                            activeDocument.selection ["one", "two", "three"];

                             

                             

                            I'll try the note suggestion though.

                            • 11. Re: selected property == inherently slow ?
                              Silly-V Adobe Community Professional

                              Why don't you just go backwards and remove selection items you do not want? This seems to be the best solution from the description you gave there.

                              • 12. Re: selected property == inherently slow ?
                                De Pascalis Level 1

                                I'm unsure but is this what you mean?

                                 

                                The de-selection is now within the loop which guaranties it to be slow.

                                 

                                var aDoc = activeDocument;
                                var sn = aDoc.selection;
                                
                                for (var i = 0; i < sn.length; i++) {
                                  var boundsArea = sn[i].width * sn[i].height;
                                  if ( boundsArea >= 0.05 ) {
                                    if ( sn[i].typename !== "PathItem" || sn[i].typename !== "CompoundPathItem" ) {
                                      sn[i].selected = false;
                                    }
                                  }
                                }
                                
                                • 13. Re: selected property == inherently slow ?
                                  CarlosCanto Adobe Community Professional & MVP

                                  hello De Pascalis, as with the original poster, is selecting your ultimate goal? if so, W_J_T suggested moving items to a temp layer, selecting such layer, then moving back items to their original spot.

                                   

                                  also, tagging your objects then selecting them with an action as previously suggested, has the benefit of being able to play actions now in newer versions through scripting for a fully automated solution.

                                  • 14. Re: selected property == inherently slow ?
                                    Silly-V Adobe Community Professional

                                    You have got to go backwards like this:

                                    for(var i = doc.selection.length - 1; i > -1; i--)

                                     

                                    But I'm interested to see if de-selection will indeed be as slow.

                                    • 15. Re: selected property == inherently slow ?
                                      De Pascalis Level 1

                                      Hi CarlosCanto

                                      My ultimate goal is to clean out path leftovers from a CAD source. The type of leftovers I'm targeting are paths with all its points overlapping each other. But I initially just want the targeted paths selected  so I can review the selection and then hit the delete button manually.

                                      I don't want to do anything to the paths from a loop as I assume that I wont be able to undo the whole script afterwards, I guess I would have to undo each iteration of the loop and every change from within the loop?

                                       

                                      I need the script to be as optimized as possible since the CAD exports are messy with a lot of paths in them to iterate through. And I can't make better exports since I don't have access to the CAD source.

                                       

                                      There are some scripts out there already that do this but they are to slow so I wanted to se if I could do better.

                                      Also, as I now got started with scripts and probably will do many more in the future, I thought learning how to optimize selection from the start was a good idea since that would probably be used a lot.

                                       

                                       

                                      Silly-V

                                      Ah ok. I'll try that but I don't think it would matter, but I learnt something by that line anyway so thanks.

                                      It's not the loop that is slow. When I set break points before and after the loop, everything runs well until it hits line 21 aDoc.selection = filteredSn; which is outside of the loop in my first example.

                                      My second example where I modify the selected property of the items instead was "equally" slow.

                                       

                                      I tried using the note property and then manually ran a "select with notes" action, that was indeed much faster (instant). I didn't have time to put the execution of the action from my script though before going home. I couldn't get Application.doScript to work but I will on the next try.

                                       

                                      I think the tagging with notes will do for now if there are no cleaner solutions available. Thanks for your help everybody!