Article View: adobe.scripting.indesign
Article #2245Re: [JS] :: [CSX] - PageItem Stacking Order
From: Peter Kahrel
Date: Wed, 07 Jan 2009 09:49
Date: Wed, 07 Jan 2009 09:49
56 lines
3441 bytes
3441 bytes
Harbs.
I knew that arrays process much quicker than collections, but didn't know that .slice(0) sped up things even more -- thanks very much for the pointer. But I can't reproduce that. I did a test similar to yours (a document with 400 pages, a text frame on each page, then tested if each object was a text frame), and it turned out that adding .everyItem().getElements() reduced the script's running time by a factor 20, which is what you found, too, but then adding .slice(0) didn't matter at all (see timings below). Maybe any fractional advantage in processing the array is cancelled by the overhead created by .slice(). So it looks as if Kris is not entirely correct when he says
The '.slice(0)' is there to force a duplication of the array. If it were omitted, InDesign would merely copy a reference to theRealm.allPageItems, and we'd be no better off.
Maybe the differences are due to the type of test (.slice(0) may have a distinct advantage on longer/shorter arrays) or type of computer (you can measure the difference, I can't). Interestingly, Kris adds another optimalisation by assigning the array's length to a variable:
var realmItems = theRealm.allPageItems.slice(0);
var realmLength = realmItems.length;
for (var idx = 0; idx < realmLength; idx++)
This has long been known to speed up array-processing, but again its effect turns out to depend on the type of test and computer. The speed tests show that collections benefit, but arrays don't always. On my PC, assigning array length to a variable and using that in the loop doesn't make any difference, but on an older computer that I used previously, which was much slower (0.8 vs 3.2 GHz), it did make a big difference: it halved an 'array-intensive' script's running time.
Anyway, here are some timings. 400-page document, text frame on each page, document has no undo-stack. Timings are highest and lowest of ten runs. Time differences such as 0.14 vs. 0.12 can be considered meaningless.
Peter
3.6-3.7 (seconds)
t = app.activeDocument.textFrames;
for (i = 0; i < t.length; i++)
if (t[i] instanceof TextFrame) {}
0.06-0.11
t = app.activeDocument.textFrames.everyItem().getElements();
0.06-0.14
t = app.activeDocument.textFrames.everyItem().getElements().slice(0);
2.5-2.6
t = app.activeDocument.textFrames;
t_length = t.length;
for (i = 0; i < t_length; i++)
if (t[i] instanceof TextFrame) {}
0.06-0.14
t = app.activeDocument.textFrames.everyItem().getElements();
0.06-0.12
t = app.activeDocument.textFrames.everyItem().getElements().slice(0);
PS: there have been claims that there might be a speed difference between 'x instance of y' and 'x.constructor.name == "y"', but that's not borne out by repeating the above tests using constructor.name.
P.
Technical Details
Message-ID:
<59b767cf.16@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!newshub.sdsu.edu!webcrossingReferences:
<59b767cf.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,5dfad1e4d169194d X-Google-Attributes: gidf939a,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Peter Kahrel" <kahrel@kahrel.plus.com> Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!newshub.sdsu.edu!webcrossing Newsgroups: adobe.scripting.indesign Subject: Re: [JS] :: [CSX] - PageItem Stacking Order Message-ID: <59b767cf.16@webcrossing.la2eafNXanI> Date: Wed, 7 Jan 2009 01:49:51 -0800 References: <59b767cf.-1@webcrossing.la2eafNXanI> Lines: 70 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit