🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #15029

JS: speeding up scripts

#15029
From: Peter_Kahrel
Date: Sun, 04 Jan 2004 20:58
25 lines
2117 bytes
Two ways to speed up Javascripts are as follows. (This is evident for programmers but I had forgotten about it until I started using a script I wrote today that was much too slow to be of any use. The changes described here made it pretty fast so I thought I'd share this.) It appears to be a good idea to avoid repeated calls to ID values, and store an ID value in a user variable and use that instead. So instead of

par = app.selection[0].parentStory.paragraphs;
for( i = 0 ; i < par.length ; i++ )
   do something

You could also write this:

par = app.selection[0].parentStory.paragraphs;
parLength = par.length;
for( i = 0 ; i < parLength ; i++ )
   do something

in which the length of the array is stored in parLength, which is used instead of the repeated call to par.length. The difference? Running through a 500-word list doing something with the contents of each paragraph takes 2.7 seconds using the first method, but 1.2 seconds using the second method: the second method is more than twice as fast. In longer scripts that use lots of calls to e.g. the length of an array, or the contents of a line/selection, etc., the speed gain can be enormous.

Another, more modest, timesaver turns out to be to use lines whenever you can instead of paragraphs. So writing

par = app.selection[0].parentStory.lines;
parLength = par.length;
for( i = 0 ; i < parLength ; i++ )
   do something

takes off anothe 0.1 second. Not a lot, but when you process large amounts of data the difference begins to tell.

Peter
Technical Details
Message-ID: <2ccf2f95.-1@webx.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!webx

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,35553e0f8aac2976,start
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2004-01-04 13:07:46 PST
From: Peter_Kahrel@adobeforums.com
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!webx
Newsgroups: adobe.scripting.indesign
Subject: JS: speeding up scripts
Message-ID: <2ccf2f95.-1@webx.la2eafNXanI>
Date: Sun, 4 Jan 2004 12:58:21 -0800
Lines: 38
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit